Java 中 C 的 printf %g 格式说明符的等价物是什么?
我尝试使用 Formatter.format,但这似乎将尾数留在尾数为 0 的数字上,而 C 版本却没有。 Java 中是否有相当于 C 的 %g 格式说明符,如果没有,是否有办法伪造它? 出于兼容性原因,我的目的是像 C 那样保留尾数。
foo.c
#include <stdio.h>
int main (int argc, char const *argv[])
{
printf("%g\n", 1.0);
return 0;
}
Main.java
class Main {
public static void main(String[] args) {
System.out.printf("%g\n", 1.0);
}
}
控制台:
$ javac Main.java && java Main
1.00000
$ gcc foo.c && ./a.out
1
同样,以 1.2 作为输入,Java 版本中的尾数更长
$ javac Main.java && java Main
1.20000
$ gcc foo.c && ./a.out
1.2
I tried using Formatter.format, but that seems to leave the mantissa on numbers with 0 mantissa, whereas the C version does not. Is there an equivalent of C's %g format specifier in Java, and if not, is there a way to fake it? My intention is to preserve the mantissa exactly like C's for compatibility reasons.
foo.c
#include <stdio.h>
int main (int argc, char const *argv[])
{
printf("%g\n", 1.0);
return 0;
}
Main.java
class Main {
public static void main(String[] args) {
System.out.printf("%g\n", 1.0);
}
}
Console:
$ javac Main.java && java Main
1.00000
$ gcc foo.c && ./a.out
1
Similarly, with 1.2 as input, the mantissa is longer in Java's version
$ javac Main.java && java Main
1.20000
$ gcc foo.c && ./a.out
1.2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
编辑:
如果指数是 17 位数字,此代码会导致小数部分丢失,因为我误解了 String.format 如何格式化这些数字。 所以请不要使用此代码:P
感谢您的输入,伙计们。 我找不到配置 DecimalFormat 或 NumberFormat 来精确克隆功能的方法,但似乎此方法有效(后面是一个示例):
main.c
Main.java
及其输出:
edit:
This code causes the fractional part to go missing if the exponent is 17 digits, because of my misunderstanding of how String.format formatted those numbers. So don't please don't use this code :P
Thanks for the input, guys. I couldn't find a way to configure DecimalFormat or NumberFormat to exactly clone the functionality, but it seems this method works (followed by an example):
main.c
Main.java
and their outputs:
您是否尝试过 java.text.DecimalFormat 类?
输出:
而:
输出:
Have you tried the java.text.DecimalFormat class?
outputs:
whereas:
outputs:
您可以使用 NumberFormat。 通过将最小小数位数设置为 0,但将最大小数位数设置得更大,它应该可以满足您的要求。
它不像 printf 那么容易,但它应该可以工作。
You could use a NumberFormat. By setting the minimum fraction digits to 0, but leaving the maximum bigger, it should do what you want.
It's not as easy as printf, but it should work.
好吧,您可以指定所需的位数:“%.2g”会将 1.2 显示为 1.20
well, you can specify how many digits you want: "%.2g" will display 1.2 as 1.20
请注意,对于 1.2345e-20,使用
失败(从末尾去除零,生成 1.2345e-2)。
这是我最终在代码中使用的内容:
基本上,我修改了表达式以确保仅删除小数部分末尾的零,并将其分为两种情况。 但感谢您为我指明了正确的方向。
Note that using
fails for 1.2345e-20 (strips the zero from the end, producing 1.2345e-2).
Here is what I ended up using in my code:
Basically I modified the expression to ensure only zeros at the end of the fractional part are removed and split this into two cases. But thanks for pointing me in the right direction.
上述解决方案都不是完美的。 在某些情况下,到处都会出现很多问题。 因此,我创建了一个小型库 Double2String 来解决这个特定问题:
https://github.com/coconut2015/double2string
None of the above solutions are prefect. There are quite a few issues here and there in certain cases. So I created a small library Double2String to address this specific issue:
https://github.com/coconut2015/double2string