当前位置: 代码迷 >> Java相关 >> [求助]java中有没有取小数点后两位的函数?
  详细解决方案

[求助]java中有没有取小数点后两位的函数?

热度:222   发布时间:2006-07-14 23:55:18.0
[求助]java中有没有取小数点后两位的函数?

如题

搜索更多相关的解决方案: java  函数  小数点  

----------------解决方案--------------------------------------------------------
something for you
[CODE]

// The 0 symbol shows a digit or 0 if no digit present
NumberFormat formatter = new DecimalFormat("000000");
String s = formatter.format(-1234.567); // -001235
// notice that the number was rounded up

// The # symbol shows a digit or nothing if no digit present
formatter = new DecimalFormat("##");
s = formatter.format(-1234.567); // -1235
s = formatter.format(0); // 0
formatter = new DecimalFormat("##00");
s = formatter.format(0); // 00


// The . symbol indicates the decimal point
formatter = new DecimalFormat(".00"); // pay attention here, that is what you want
s = formatter.format(-.567); // -.57
formatter = new DecimalFormat("0.00");
s = formatter.format(-.567); // -0.57
formatter = new DecimalFormat("#.#");
s = formatter.format(-1234.567); // -1234.6
formatter = new DecimalFormat("#.######");
s = formatter.format(-1234.567); // -1234.567
formatter = new DecimalFormat(".######");
s = formatter.format(-1234.567); // -1234.567
formatter = new DecimalFormat("#.000000");
s = formatter.format(-1234.567); // -1234.567000


// The , symbol is used to group numbers
formatter = new DecimalFormat("#,###,###");
s = formatter.format(-1234.567); // -1,235
s = formatter.format(-1234567.890); // -1,234,568

// The ; symbol is used to specify an alternate pattern for negative values
formatter = new DecimalFormat("#;(#)");
s = formatter.format(-1234.567); // (1235)

// The ' symbol is used to quote literal symbols
formatter = new DecimalFormat("'#'#");
s = formatter.format(-1234.567); // -#1235
formatter = new DecimalFormat("'abc'#");
s = formatter.format(-1234.567); // -abc1235


[/CODE]
----------------解决方案--------------------------------------------------------
谢谢楼上的大哥
----------------解决方案--------------------------------------------------------
  相关解决方案