[英]DecimalFormat not working properly when input is xx.x
我有下面的代碼DecimalFormat
。
import java.util.*;
import java.lang.*;
import java.text.*;
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
double d = 50.12345;
DecimalFormat df = new DecimalFormat("#.##");
System.out.println(df.format(d));
d = 50.0;
df = new DecimalFormat("#.##");
System.out.println(df.format(d));
d = (double) (1*1.000/Integer.parseInt(2+"")/1.000*100);
df = new DecimalFormat("#.##");
System.out.print(df.format(d));
}
}
這給我輸出如下。
50.12
50
50
我面臨的問題是第二個問題。 我想將其打印為50.00。
知道我該怎么做嗎?
使用0.00作為格式。 #表示一個可選數字。
您可以在這里找到所有格式代碼及其含義: http : //docs.oracle.com/javase/7/docs/api/java/text/DecimalFormat.html
用這個
double d = 50.12345;
DecimalFormat df = new DecimalFormat("0.00");
System.out.println(df.format(d));
d = 50.0;
System.out.print(df.format(d));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.