Java 浮动 123.129456 到 123.12 没有四舍五入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5051395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java float 123.129456 to 123.12 without rounding
提问by Mat B.
How do you cut down floatprimitive in java to two decimalplaces, without using rounding?:
如何在不使用舍入的情况下将java 中的float原语减少到两位小数?:
123.99999 to 123.99
-8.022222 to -8.02
There should be no rounding just cut of the decimal places and leave two.
不应该四舍五入,只截取小数位并保留两位。
Second point is how do you validateor count how many decimals are after the point?:
第二点是你如何验证或计算点后有多少小数?:
123.99 will give true or 2
123.999 will give false or 3
UPDATE
更新
The numbers are String input so I think I will go with this as suggested; and I'll just have int try/catch block for any exceptions. Any suggestions how to make this work any smarter way are welcome:
数字是字符串输入,所以我想我会按照建议去做;对于任何异常,我都会使用 int try/catch 块。欢迎任何有关如何以更智能的方式进行这项工作的建议:
public static float onlyTwoDecimalPlaces(String number) {
StringBuilder sbFloat = new StringBuilder(number);
int start = sbFloat.indexOf(".");
if (start < 0) {
return new Float(sbFloat.toString());
}
int end = start+3;
if((end)>(sbFloat.length()-1)) end = sbFloat.length();
String twoPlaces = sbFloat.substring(start, end);
sbFloat.replace(start, sbFloat.length(), twoPlaces);
return new Float(sbFloat.toString());
}
采纳答案by aioobe
First of all, there is no guarantee that just because a float can represent a.bcde
exactly, it is guaranteed to be able to represent a.bc
exactly.
首先,不能保证仅仅因为一个浮点数可以a.bcde
精确表示,就保证它能够a.bc
精确表示。
So if you're after just the printing part, how about doing it with some string-manipulation? Find the decimal point using indexOf
and extract the part with two decimals, using substring
.
因此,如果您只关注打印部分,那么通过一些字符串操作来完成它如何?使用找到小数点indexOf
并使用两位小数提取部分substring
。
回答by Udi Reshef
When you use DecimalFormat
be aware to the fact that many languages uses "," instead of "." for float. So while you will format your float to "0.00" it will become "0,00" in certain locales (such as German and Polish). This will cause a NullPointerException
while you will use this new formatted float in android applications. So what I did in order to cut and not round is to cast it to int after multiply it with 100 then recast it back to float and divide it to 100
This is the line:
当您使用时请DecimalFormat
注意许多语言使用“,”而不是“。”的事实。为浮动。因此,当您将浮点数格式化为“0.00”时,它会在某些语言环境(例如德语和波兰语)中变为“0,00”。这将导致NullPointerException
您将在 android 应用程序中使用这个新的格式化浮点数。所以我为了切割而不是圆而做的是在将它乘以 100 后将其转换为 int 然后将其重新转换为浮动并将其除以 100 这是该行:
myFloat = (float)((int)( myFloat *100f))/100f;
You can try it with log:
你可以用日志试试:
float myFloat = 12.349;
myFloat = (float)((int)( myFloat *100f ))/100f;
Log.d(TAG, " myFloat = "+ myFloat); //you will get myFloat = 12.34
This will cut myFloat two places after the decimal point to format of ("0.00")
it will not round it like this line (myFloat = Math.round(myFloat *100.0)/100.0;
) it will just cut it.
这会将 myFloat 小数点后两位削减为 ("0.00") 的格式,它不会像这条线 ( myFloat = Math.round(myFloat *100.0)/100.0;
)那样舍入它,它只会削减它。
回答by Pablo Santa Cruz
Do keep in mind that floatare floating pointvalues. So there might not even be an exact two decimalrepresentation for a certain number.
请记住,float是浮点值。因此,对于某个数字,甚至可能没有精确的两位十进制表示。
Having said that, you might try something like:
话虽如此,您可以尝试以下操作:
float f = -8.022222f;
BigDecimal bd = new BigDecimal(f);
BigDecimal res = bd.setScale(2, RoundingMode.HALF_UP);
f = res.floatValue();
System.out.println(f);
You might need to use a different RoundingMode
though. Depends on what you want.
不过,您可能需要使用不同的方法RoundingMode
。取决于你想要什么。
回答by Quang
The easy way to use DecimalFormat:
使用 DecimalFormat 的简单方法:
DecimalFormat df = new DecimalFormat("0.00");
float f = -8.0222222f;
f = Float.parseFloat(df.format(f));
System.out.println(f);
回答by Jegan
Please try this, this one works for you.
请试试这个,这个对你有用。
double d = 16.66667;
DecimalFormat decimalFormat= new DecimalFormat("#.##");
decimalFormat.setRoundingMode(RoundingMode.FLOOR);
System.out.println("Result :"+decimalFormat.format(d));