2017-04-21 120 views
1

我在货币对象中获得价值,但我只想要双重格式。如何将货币格式转换为java中的两倍

String amt = txn.getAmount(); 
System.out.println("--amt--"+amt);//output:1010 
double value = Double.parseDouble(amt); 
System.out.println("---value---"+value);//output:1010.0 
String ammount=NumberFormat.getCurrencyInstance().format(value); 
System.out.println("--ammount--"+ammount);//output:Rs.1,010.00 

在这里,我想向Rs.1,010.001010.00

在我的代码的任何错误呢?

+2

的可能的复制[?如何显示浮点数据在Java 2位小数输出(http://stackoverflow.com/questions/2538787/how-to-display -an-output-of-float-data-with-2-decimal-places-in-java) –

+0

你为什么要'getAmount'返回一个字符串? –

+0

@ cricket_007:不重复请仔细观察, – Durga

回答

2

我假设你做要使用的货币细节。在这种情况下,请使用getNumberInstance()而不是getCurrencyInstance()。

用途:

NumberFormat nf = NumberFormat.getNumberInstance(); 
nf.setGroupingUsed(false); 
nf.setMinimumFractionDigits(2); 
String ammount= nf.format(value); 
0

打印之前,请替换字符串“Rs”。与“”以及“,”与“”。

String replaceString1=amount.replace("Rs.",""); 
String replaceString2=amount.replace(",",""); 

这是一种处理这种情况的方法。 希望这有助于。

+0

“。”的用法。和“,”是语言特定的。在其他语言中使用“。”和“,”可能会翻转。 – MalaKa

+0

这只是针对作者提出查询的情况(特定语言)。 –

+0

这里真正的解决方案是根本不使用Currency实例 –

0

试试这个更清洁的方法!

 double d = 1010.00; 
     Locale uk = new Locale("en", "GB"); 
     NumberFormat cf = NumberFormat.getCurrencyInstance(uk); 
     String s = cf.format(d); 

     System.out.println(s); 

     Number number = null; 
     try 
     { 
     number = cf.parse(s); 
     } 
     catch (ParseException e) 
     { 
     System.out.print(e); 
     } 
     double dClone = number.doubleValue(); 
+0

这对于“干净” –

+0

非常详细哈哈,我认为在其他hacky解决方案方面它更干净,如果您有更好的发言权,请考虑修复此问题:D –

+0

我已经标记了重复 –