在Java中,如何删除float中的所有0?

发布于 2024-12-19 02:50:43 字数 143 浏览 2 评论 0原文

我想这样改变浮动:

10.5000 -> 10.5 10.0000 -> 10.0000 -> 10

如何删除小数点后的所有零,并将其更改为 float(如果有非零)或 int(如果只有零)?

提前致谢。

I'd like to change float like this way:

10.5000 -> 10.5
10.0000 -> 10

How can I delete all zeros after the decimal point, and change it either float (if there's non-zeros) or int (if there were only zeros)?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

溺深海 2024-12-26 02:50:43

为什么不尝试正则表达式呢?

new Float(10.25000f).toString().replaceAll("\\.?0*$", "")

Why not try regexp?

new Float(10.25000f).toString().replaceAll("\\.?0*$", "")
不忘初心 2024-12-26 02:50:43

诀窍是浮点数和双精度数本身实际上并没有尾随零;这只是它们的打印方式(或初始化为文字)可能会显示它们。考虑这些示例:

Float.toString(10.5000); // => "10.5"
Float.toString(10.0000); // => "10.0"

您可以使用 DecimalFormat 修复“10.0”的示例:

new java.text.DecimalFormat("#").format(10.0); // => "10"

Well the trick is that floats and doubles themselves don't really have trailing zeros per se; it's just the way they are printed (or initialized as literals) that might show them. Consider these examples:

Float.toString(10.5000); // => "10.5"
Float.toString(10.0000); // => "10.0"

You can use a DecimalFormat to fix the example of "10.0":

new java.text.DecimalFormat("#").format(10.0); // => "10"
黯淡〆 2024-12-26 02:50:43

java.math.BigDecimal 有一个 stripTrailingZeros() 方法,它将实现您正在寻找的内容。

BigDecimal myDecimal = new BigDecimal(myValue);
myDecimal.stripTrailingZeros();
myValue = myDecimal.floatValue();

java.math.BigDecimal has a stripTrailingZeros() method, which will achieve what you're looking for.

BigDecimal myDecimal = new BigDecimal(myValue);
myDecimal.stripTrailingZeros();
myValue = myDecimal.floatValue();
静谧 2024-12-26 02:50:43

这使用两种不同的格式化程序来处理它:

double d = 10.5F;
DecimalFormat formatter = new DecimalFormat("0");
DecimalFormat decimalFormatter = new DecimalFormat("0.0");
String s;
if (d % 1L > 0L) s = decimalFormatter.format(d);
else s = formatter.format(d);

System.out.println("s: " + s);

This handles it with two different formatters:

double d = 10.5F;
DecimalFormat formatter = new DecimalFormat("0");
DecimalFormat decimalFormatter = new DecimalFormat("0.0");
String s;
if (d % 1L > 0L) s = decimalFormatter.format(d);
else s = formatter.format(d);

System.out.println("s: " + s);
白日梦 2024-12-26 02:50:43

您只需要使用如下格式类:

new java.text.DecimalFormat("#.#").format(10.50000);

You just need to use format class like following:

new java.text.DecimalFormat("#.#").format(10.50000);
痕至 2024-12-26 02:50:43

根据需要格式化输出的数字。您无法删除内部“0”值。

Format your numbers for your output as required. You cannot delete the internal "0" values.

抚笙 2024-12-26 02:50:43

尝试使用 System.out.format

这是一个允许 c 样式格式的链接
http://docs.oracle.com/javase/tutorial/java/data /numberformat.html

Try using System.out.format

Heres a link which allows c style formatting
http://docs.oracle.com/javase/tutorial/java/data/numberformat.html

冷情妓 2024-12-26 02:50:43

我遇到了同样的问题,并在以下链接中找到了解决方法:
StackOverFlow - 如何良好地格式化浮点数字符串没有不必要的小数 0

JasonD 的答案是我遵循的答案。它不依赖于区域设置,这对我的问题有好处,并且对于长值没有任何问题。

希望这有帮助。

从上面的链接添加内容:

public static String fmt(double d) {
    if(d == (long) d)
        return String.format("%d",(long)d);
    else
        return String.format("%s",d);
    }

产生:

232
0.18
1237875192
4.58
0
1.2345

I had the same issue and find a workaround in the following link:
StackOverFlow - How to nicely format floating numbers to string without unnecessary decimal 0

The answer from JasonD was the one I followed. It's not locale-dependent which was good for my issue and didn't have any problem with long values.

Hope this help.

ADDING CONTENT FROM LINK ABOVE:

public static String fmt(double d) {
    if(d == (long) d)
        return String.format("%d",(long)d);
    else
        return String.format("%s",d);
    }

Produces:

232
0.18
1237875192
4.58
0
1.2345
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文