当前位置: 技术问答>java相关
如何输出小数时按规定的位数输出。。
来源: 互联网 发布时间:2015-02-02
本文导语: 如 x=12.34 OutNum(x,2) == 12.34 OutNum(x,3) == 12.340 OutNum(x,4) == 12.3400 | import java.text.DecimalFormat; String a = new DecimalFormat("###,###,###.##").format(100.5 ); System.out.println("a="+a); 结果: a=100.50 | pub...
如
x=12.34
OutNum(x,2) == 12.34
OutNum(x,3) == 12.340
OutNum(x,4) == 12.3400
x=12.34
OutNum(x,2) == 12.34
OutNum(x,3) == 12.340
OutNum(x,4) == 12.3400
|
import java.text.DecimalFormat;
String a = new DecimalFormat("###,###,###.##").format(100.5 );
System.out.println("a="+a);
结果:
a=100.50
String a = new DecimalFormat("###,###,###.##").format(100.5 );
System.out.println("a="+a);
结果:
a=100.50
|
public class numformat
{
static String num(String xx, String ii)
{
String str = new String();
try
{
float x= Float.parseFloat(xx);
int i = Integer.parseInt(ii);
if (i -1)
{
str = xx.substring(0, xx.indexOf("."));
}
else
{
str = xx;
}
return str;
}
if (xx.indexOf(".") > -1)
{
int f = xx.substring(xx.indexOf(".")+1, xx.length()).length();
if (f
{
static String num(String xx, String ii)
{
String str = new String();
try
{
float x= Float.parseFloat(xx);
int i = Integer.parseInt(ii);
if (i -1)
{
str = xx.substring(0, xx.indexOf("."));
}
else
{
str = xx;
}
return str;
}
if (xx.indexOf(".") > -1)
{
int f = xx.substring(xx.indexOf(".")+1, xx.length()).length();
if (f