float price;
int quantity;
double s = price*quantity;
两个变量相乘后有7位小数 怎么用DecimalFormat类来控制结果到2位小数 如果输入整数如:500也要能转成500.00
int quantity;
double s = price*quantity;
两个变量相乘后有7位小数 怎么用DecimalFormat类来控制结果到2位小数 如果输入整数如:500也要能转成500.00
解决方案 »
- Java 关于线程共享变量的疑问
- 问个java 的菜鸟问题!!!请各位高手拔刀相助!
- 使用jacob时总是报错“com.jacob.com.ComFailException: Can't get object clsid from progid”这是怎么回事?
- 怎样判别一个字符中否中文
- dom4j的格式?
- 请教JAVA打印发票设置纸张大小奇怪的问题
- 有谁知道windows的环境变量是保存在哪里的?
- Spring事务配置和程序代码结构都正确,为什么事务就是不起作用。实在想不出哪里有问题。
- 请问在哪里可以下载JBuilder6的中文教材?
- 谁搞的定???
- 如何换算金额大小写??
- 多个小数点比较的问题
df.applyPattern("#0.00");float price=123123.23423432;
int quantity=3;
double s = price*quantity;
String sh=df.format(s);
System.out.println(sh);
/**
* 保留小数点后2位
*
* @param value
* @return
*/
public static String number2String(Number value) {
value = value == null ? 0 : value;
DecimalFormat a = new DecimalFormat("#.##");
a.applyPattern("0.00");
return a.format(value);
}
df.applyPattern("#0.00"); float price=123123.23423432F;
int quantity=3;
double s = price*quantity;
String sh=df.format(s);
System.out.println(sh);
我忘了加个F了float price=123123.23423432F;
public class DemoClass {
public static void main(String[] args) {
NumberFormat nf=new DecimalFormat("#0.00");
int quantity=3;
int s = 300*quantity;
System.out.println(nf.format(s));
}
}