小数点后如何制作一位数?

时间:2021-07-29 17:07:15

Searching how to make two digit after decimal point as below

搜索如何使小数点后的两位数如下所示

$ = (double) (((int)($ * 100)) / 100.0);

Anyone can help to how to make "one digit" after decimal point ?

任何人都可以帮助如何在小数点后制作“一位数”?

thanks a lot

非常感谢

2 个解决方案

#1


0  

What about this:

那这个呢:

$ = (double) (((int)($ * 10)) / 10.0);

Note: edited after the comment. If you need java and rounding instead of truncate try this:

注意:评论后编辑。如果你需要java和舍入而不是truncate试试这个:

import java.lang.Math;

double a;
a = ((double) (round(a*10))) / 10.0;

This will shift left the dot of one position, round to the nearest integer and then shift the dot back to the right one position.

这将向左移动一个位置的点,舍入到最接近的整数,然后将点移回到右边的一个位置。

Edited second time:

第二次编辑:

What you need is still unclear. If you are fine with truncate as before:

你需要的还不清楚。如果你像以前一样使用truncate:

double $=1.24*(double)amount;
$ = (double) (((int)($ * 1000)) / 1000.0);
outelc.setText("ELC(1.24)= " + Double.toString($) + " /pc");

If you need rounding:

如果你需要四舍五入:

double $=1.24*(double)amount;
$ = (double) ((round($ * 1000)) / 1000.0);
outelc.setText("ELC(1.24)= " + Double.toString($) + " /pc");

#2


0  

If you are using doubles to store/calculate currency values, then you will likely find yourself in a world of pain with rounding. Been there, done that, got the scars.

如果您使用双打来存储/计算货币值,那么您可能会发现自己陷入了四舍五入的痛苦世界。去过那里,做到了,得到了伤疤。

I highly recommend that you use BigDecimal values for ALL currency values, and do not even involve doubles in the instantiation. Always use the String constructor.

我强烈建议您对所有货币值使用BigDecimal值,甚至在实例化中不涉及双精度数。始终使用String构造函数。

See related questions here and here.

请在此处和此处查看相关问题。

#1


0  

What about this:

那这个呢:

$ = (double) (((int)($ * 10)) / 10.0);

Note: edited after the comment. If you need java and rounding instead of truncate try this:

注意:评论后编辑。如果你需要java和舍入而不是truncate试试这个:

import java.lang.Math;

double a;
a = ((double) (round(a*10))) / 10.0;

This will shift left the dot of one position, round to the nearest integer and then shift the dot back to the right one position.

这将向左移动一个位置的点,舍入到最接近的整数,然后将点移回到右边的一个位置。

Edited second time:

第二次编辑:

What you need is still unclear. If you are fine with truncate as before:

你需要的还不清楚。如果你像以前一样使用truncate:

double $=1.24*(double)amount;
$ = (double) (((int)($ * 1000)) / 1000.0);
outelc.setText("ELC(1.24)= " + Double.toString($) + " /pc");

If you need rounding:

如果你需要四舍五入:

double $=1.24*(double)amount;
$ = (double) ((round($ * 1000)) / 1000.0);
outelc.setText("ELC(1.24)= " + Double.toString($) + " /pc");

#2


0  

If you are using doubles to store/calculate currency values, then you will likely find yourself in a world of pain with rounding. Been there, done that, got the scars.

如果您使用双打来存储/计算货币值,那么您可能会发现自己陷入了四舍五入的痛苦世界。去过那里,做到了,得到了伤疤。

I highly recommend that you use BigDecimal values for ALL currency values, and do not even involve doubles in the instantiation. Always use the String constructor.

我强烈建议您对所有货币值使用BigDecimal值,甚至在实例化中不涉及双精度数。始终使用String构造函数。

See related questions here and here.

请在此处和此处查看相关问题。