草庐IT

java - 如何以货币格式格式化小数?

有没有办法将小数格式化如下:100->"100"100.1->"100.10"如果是整数,则省略小数部分。否则格式为两位小数。 最佳答案 我建议使用java.text包:doublemoney=100.1;NumberFormatformatter=NumberFormat.getCurrencyInstance();StringmoneyString=formatter.format(money);System.out.println(moneyString);这具有特定于区域设置的额外好处。但是,如果必须,截断返回的字符串(如果

java - 只显示小数点后两位

这个问题在这里已经有了答案:howtoconvertdoubleto2numberafterthedot?[duplicate](6个回答)关闭9年前。如何得到只有小数点后两位的double值。例如如果i=348842.doublei2=i/60000;tv.setText(String.valueOf(i2));此代码生成5.81403333。但我只想要5.81。那我该怎么办? 最佳答案 使用DecimalFormat.DecimalFormatisaconcretesubclassofNumberFormatthatformat

java - 只显示小数点后两位

这个问题在这里已经有了答案:howtoconvertdoubleto2numberafterthedot?[duplicate](6个回答)关闭9年前。如何得到只有小数点后两位的double值。例如如果i=348842.doublei2=i/60000;tv.setText(String.valueOf(i2));此代码生成5.81403333。但我只想要5.81。那我该怎么办? 最佳答案 使用DecimalFormat.DecimalFormatisaconcretesubclassofNumberFormatthatformat

java - 将 float 四舍五入到小数点后两位的最佳做法是什么?

这个问题在这里已经有了答案:HowtoroundanumbertondecimalplacesinJava(39个回答)关闭5年前。我正在使用eclipse+AndroidSDK。我需要将浮点值四舍五入到小数点后两位。我通常使用数学库使用下一个“技巧”。floataccelerometerX=accelerometerX*100;accelerometerX=round(accelerometerX);Log.d("Test",""+accelerometerX/100);但我觉得这不是最好的方法。是否有库可以执行这些类型的操作? 最佳答案

java - 将 float 四舍五入到小数点后两位的最佳做法是什么?

这个问题在这里已经有了答案:HowtoroundanumbertondecimalplacesinJava(39个回答)关闭5年前。我正在使用eclipse+AndroidSDK。我需要将浮点值四舍五入到小数点后两位。我通常使用数学库使用下一个“技巧”。floataccelerometerX=accelerometerX*100;accelerometerX=round(accelerometerX);Log.d("Test",""+accelerometerX/100);但我觉得这不是最好的方法。是否有库可以执行这些类型的操作? 最佳答案

java - 用逗号作为小数分隔符解析Double的最佳方法?

因为comma用作decimalseparator,此代码抛出NumberFormatException:Stringp="1,234";Doubled=Double.valueOf(p);System.out.println(d);有没有比解析"1,234"得到1.234更好的方法:p=p.replaceAll(",",".");? 最佳答案 使用java.text.NumberFormat:NumberFormatformat=NumberFormat.getInstance(Locale.FRANCE);Numbernumbe

java - 用逗号作为小数分隔符解析Double的最佳方法?

因为comma用作decimalseparator,此代码抛出NumberFormatException:Stringp="1,234";Doubled=Double.valueOf(p);System.out.println(d);有没有比解析"1,234"得到1.234更好的方法:p=p.replaceAll(",",".");? 最佳答案 使用java.text.NumberFormat:NumberFormatformat=NumberFormat.getInstance(Locale.FRANCE);Numbernumbe

java - 将 BigDecimal 舍入为 *always* 有两位小数

我正在尝试将BigDecimal值向上舍入到小数点后两位。我正在使用BigDecimalrounded=value.round(newMathContext(2,RoundingMode.CEILING));logger.trace("rounded{}to{}",value,rounded);但它并没有始终如一地做我想要的:rounded0.819to0.82rounded1.092to1.1rounded1.365to1.4//shouldbe1.37rounded2.730to2.8//shouldbe2.74rounded0.819to0.82我不关心有效数字,我只想要两位小数

java - 将 BigDecimal 舍入为 *always* 有两位小数

我正在尝试将BigDecimal值向上舍入到小数点后两位。我正在使用BigDecimalrounded=value.round(newMathContext(2,RoundingMode.CEILING));logger.trace("rounded{}to{}",value,rounded);但它并没有始终如一地做我想要的:rounded0.819to0.82rounded1.092to1.1rounded1.365to1.4//shouldbe1.37rounded2.730to2.8//shouldbe2.74rounded0.819to0.82我不关心有效数字,我只想要两位小数

java - 将 float 格式化为 n 位小数

我需要将float格式化为“n”个小数位。正在尝试BigDecimal,但返回值不正确...publicstaticfloatRedondear(floatpNumero,intpCantidadDecimales){//thefunctioniscallwiththevaluesRedondear(625.3f,2)BigDecimalvalue=newBigDecimal(pNumero);value=value.setScale(pCantidadDecimales,RoundingMode.HALF_EVEN);//herethevalueiscorrect(625.30)re