# 如何在Java中显示2位小数的浮点数据输出？

### 11 Solutions collect form web for “如何在Java中显示2位小数的浮点数据输出？”

` `System.out.printf("%.2f", val);` `

• `d` ：十进制整数
• `o` ：八进制整数
• `e` ：浮点科学记数法

` `DecimalFormat df = new DecimalFormat(); df.setMaximumFractionDigits(2); System.out.println(df.format(decimalNumber));` `

` `float myFloat = 2.001f; String formattedString = String.format("%.02f", myFloat);` `
` `double d = 1.234567; DecimalFormat df = new DecimalFormat("#.##"); System.out.print(df.format(d));` `

` `System.out.printf("%1.2f", 3.14159D);` `

` `float f = 102.236569f; DecimalFormat decimalFormat = new DecimalFormat("#.##"); float twoDigitsF = Float.valueOf(decimalFormat.format(f)); // output is 102.24` `

` `double new_variable = Math.round(old_variable*100) / 100.0;` `

` ` DecimalFormat myFormatter = new DecimalFormat(pattern); String output = myFormatter.format(value); System.out.println(value + " " + pattern + " " + output);` `

` `float floatValue=22.34555f; System.out.print(String.format("%.2f", floatValue));` `

OK – str漂浮。

` `package test; import java.text.DecimalFormat; public class TestPtz { public static void main(String[] args) { String preset0 = "0.09,0.20,0.09,0.07"; String[] thisto = preset0.split(","); float a = (Float.valueOf(thisto[0])).floatValue(); System.out.println("[Original]: " + a); a = (float) (a + 0.01); // Part 1 - for display / debug System.out.printf("[Local]: %.2f \n", a); // Part 2 - when value requires to be send as it is DecimalFormat df = new DecimalFormat(); df.setMinimumFractionDigits(2); df.setMaximumFractionDigits(2); System.out.println("[Remote]: " + df.format(a)); } }` `

` `run: [Original]: 0.09 [Local]: 0.10 [Remote]: 0.10 BUILD SUCCESSFUL (total time: 0 seconds)` `

` `import java.io.*; import java.util.Scanner; public class twovalues { public static void main(String args[]) { float a,b; Scanner sc=new Scanner(System.in); System.out.println("Enter Values For Calculation"); a=sc.nextFloat(); b=sc.nextFloat(); float c=a/b; System.out.printf("%.2f",c); } }` `
• 格式化数字为固定宽度，前导零
• 如何在JavaScript中将数字格式设置为美元货币string？
• DateTime“Z”格式说明符在哪里？
• Java中的双十进制格式
• 如何在PHP中正确地转义html表单input默认值？
• 需要在Java中获得当前的时间戳
• 从XML格式的string资源中设置TextView文本
• 使用printf与一个非空终止的string