如何使用可变精度的Java String.format?

发布于 2024-08-22 05:43:10 字数 642 浏览 2 评论 0 原文

我想允许用户改变从 double 生成的 String 的精度。现在,我正在尝试类似的方法,

String foo = String.format("%.*f\n", precision, my_double);

但是,我收到了 java.util.UnknownFormatConversionException。我对这种方法的灵感来自于 C printf此资源(第 1.3.1 节)。

我是否在某个地方有一个简单的语法错误,Java是否支持这种情况,或者有更好的方法吗?

编辑:

我想我可以做类似的事情

String foo = String.format("%." + precision + "f\n", my_double);

,但我仍然对此类操作的本机支持感兴趣。

I'd like to allow the user to vary the precision of a String generated from a double. Right now, I'm trying something like

String foo = String.format("%.*f\n", precision, my_double);

However, I receive a java.util.UnknownFormatConversionException. My inspiration for this approach was C printf and this resource (section 1.3.1).

Do I have a simple syntax error somewhere, does Java support this case, or is there a better approach?

Edit:

I suppose I could do something like

String foo = String.format("%." + precision + "f\n", my_double);

but I'd still be interested in native support for such an operation.

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

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

发布评论

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

评论(5

北风几吹夏 2024-08-29 05:43:11

您有点回答了自己的问题 - 动态构建格式字符串...有效的格式字符串遵循此处概述的约定: http://java.sun.com/j2se/1.5.0/docs/api/java/util/Formatter.html#syntax

如果您想要一个总共占用 8 个字符(包括小数点)的格式化小数,并且您想要小数点后 4 位数字,那么您的格式字符串应该类似于“%8.4f”...

据我所知,没有“本机支持” “在 Java 中,格式字符串比灵活的更灵活。

You sort of answered your own question - build your format string dynamically... valid format strings follow the conventions outlined here: http://java.sun.com/j2se/1.5.0/docs/api/java/util/Formatter.html#syntax.

If you want a formatted decimal that occupies 8 total characters (including the decimal point) and you wanted 4 digits after the decimal point, your format string should look like "%8.4f"...

To my knowledge there is no "native support" in Java beyond format strings being flexible.

您可以使用 DecimalFormat 类。

double d1 = 3.14159;
double d2 = 1.235;

DecimalFormat df = new DecimalFormat("#.##");

double roundedD1 = df.format(d); // 3.14
double roundedD2 = df.format(d); // 1.24

如果您想在运行时设置精度,请调用:

df.setMaximumFractionDigits(precision)

You can use the DecimalFormat class.

double d1 = 3.14159;
double d2 = 1.235;

DecimalFormat df = new DecimalFormat("#.##");

double roundedD1 = df.format(d); // 3.14
double roundedD2 = df.format(d); // 1.24

If you want to set the precision at run time call:

df.setMaximumFractionDigits(precision)
顾冷 2024-08-29 05:43:11

为什么不:

String form = "%."+precision+"f\n";
String foo = String.format(form, my_double);

或:

public static String myFormat(String src, int precision, Object args...)
{
    String form = "%."+precision+"f\n";
    return String.format(form, args);
}

Why not :

String form = "%."+precision+"f\n";
String foo = String.format(form, my_double);

or :

public static String myFormat(String src, int precision, Object args...)
{
    String form = "%."+precision+"f\n";
    return String.format(form, args);
}
绿光 2024-08-29 05:43:11
double pi = Math.PI; // 3.141592653589793
int n = 5;
DecimalFormat df = new DecimalFormat();
df.setMaximumFractionDigits(n);
System.out.printf(df.format(pi)); // 3.14159

您可以在运行时设置n的值。从上面的代码中给出的 n = 5 将打印 3.14159

double pi = Math.PI; // 3.141592653589793
int n = 5;
DecimalFormat df = new DecimalFormat();
df.setMaximumFractionDigits(n);
System.out.printf(df.format(pi)); // 3.14159

You can set value of n at runtime. Here from the above code given n = 5 will print 3.14159

音盲 2024-08-29 05:43:11

Willi Mentzel 解决方案针对 Java 17 进行了更新:

double d1 = 3.14159;
double d2 = 1.235;

DecimalFormat df = new DecimalFormat("#.##");

String roundedD1 = df.format(d); // 3.14
String roundedD2 = df.format(d); // 1.24

Willi Mentzel solution updated for Java 17:

double d1 = 3.14159;
double d2 = 1.235;

DecimalFormat df = new DecimalFormat("#.##");

String roundedD1 = df.format(d); // 3.14
String roundedD2 = df.format(d); // 1.24
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文