如何在Android中打印带有两位小数的双精度数?

发布于 2024-12-14 14:29:33 字数 411 浏览 7 评论 0原文

也许这是一个愚蠢的问题,但如果它不创建方法,我无法猜测如何解决它。也许有一种“自然的方式”可以做到这一点,例如 C 语言。问题是:

我有一个 var:

double a;

并且我只想用 2 或 3 位小数来显示它。当我尝试展示它时:

Text.setText("Value of a: " + String.valueOf(a));

它给出了类似的内容:

a的值:5.234966145

我想要的只是

a 的值:5.23

不改变 a 的实际值,因此它显示近似数,但适用于实数。

Maybe this is a silly question, but I cannot guess how to solve it if it's not creating a method. Maybe there's a "natural way" to do it, like in C for example. Here's the problem:

I have a var:

double a;

And I want to show it only with 2 or 3 decimals. When I try to show it:

Text.setText("Value of a: " + String.valueOf(a));

It gives something like:

Value of a: 5.234966145

And i would want just

Value of a: 5.23

Without changing the real value of a so it shows the approximate number but works with the real number.

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

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

发布评论

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

评论(5

梦晓ヶ微光ヅ倾城 2024-12-21 14:29:33
yourTextView.setText(String.format("Value of a: %.2f", a));
yourTextView.setText(String.format("Value of a: %.2f", a));
半暖夏伤 2024-12-21 14:29:33

要显示最多两位小数的数字,有两种可能性 -
1)首先,您只想显示小数位(如果有)。
例如 - i) 12.10 将显示为 12.1,ii) 12.00 将显示为 12。然后使用

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

- 2) 其次,您希望显示小数位,而不管小数点的存在 例如 -i) 12.10 将显示为 12.10。 ii) 12 显示为 12.00。然后使用-

DecimalFormat formater = new DecimalFormat("0.00"); 

For Displaying digit upto two decimal places there are two possibilities -
1) Firstly, you only want to display decimal digits if it's there.
For example - i) 12.10 to be displayed as 12.1, ii) 12.00 to be displayed as 12. Then use-

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

2) Secondly, you want to display decimal digits irrespective of decimal present For example -i) 12.10 to be displayed as 12.10. ii) 12 to be displayed as 12.00.Then use-

DecimalFormat formater = new DecimalFormat("0.00"); 
蓝梦月影 2024-12-21 14:29:33

您可以使用 DecimalFormatString.format("%.2f", a);

You can use a DecimalFormat, or String.format("%.2f", a);

深海夜未眠 2024-12-21 14:29:33

在使用 DecimalFormat 之前,您需要使用以下导入,否则您的代码将无法工作:

import java.text.DecimalFormat;

格式化的代码是:

DecimalFormat precision = new DecimalFormat("0.00"); 
// dblVariable is a number variable and not a String in this case
txtTextField.setText(precision.format(dblVariable));

Before you use DecimalFormat you need to use the following import or your code will not work:

import java.text.DecimalFormat;

The code for formatting is:

DecimalFormat precision = new DecimalFormat("0.00"); 
// dblVariable is a number variable and not a String in this case
txtTextField.setText(precision.format(dblVariable));
千紇 2024-12-21 14:29:33
textView2.setText(String.format("%.2f", result));

并且

DecimalFormat form = new DecimalFormat("0.00");
         textView2.setText(form.format(result) );

...在欧洲区域设置中导致“NumberFormatException”错误,因为它将结果设置为逗号而不是小数点 - 将 textView 添加到 editText 中的数字时会发生错误。
这两种解决方案在美国和英国地区都运行良好。

textView2.setText(String.format("%.2f", result));

and

DecimalFormat form = new DecimalFormat("0.00");
         textView2.setText(form.format(result) );

...cause "NumberFormatException" error in locale for Europe because it sets result as comma instead of point decimal - error occurs when textView is added to number in editText.
Both solutions are working excellent in locale US and UK.

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