Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个双类型数字数组 {1.0, 2.3, 3.45, 7.8, 9.0, 5.0}。输出数字时,我想让小数点为 0 的数字显示为整数:例如,1.0 => 1, 9.0 => 9。
在java中,你如何轻松做到这一点?谢谢
不使用我建议的任何库:
public static String parse(double num) { if((int) num == num) return Integer.toString((int) num); //for you, StackOverflowException return String.valueOf(num); //and for you, Christian Kuetbach }
将 double 类型转换为 int 会去掉小数位。
随意以任何您喜欢的方式将数字转换为字符串;)