草庐IT

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 - 将 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我不关心有效数字,我只想要两位小数

iphone - 将 double 限制为两位小数,不带尾随零

我读过this和that.我正是想要这个:1.4324=>"1.43"9.4000=>"9.4"43.000=>"43"9.4=>"9.40"(wrong)43.000=>"43.00"(wrong)这两个问题的答案都指向NSNumberFormatter。所以它应该很容易实现,但对我来说不是。-(void)viewDidLoad{[superviewDidLoad];UILabel*myLabel=[[UILabelalloc]initWithFrame:CGRectMake(50,100,200,20)];NSNumberFormatter*doubleValueWithMaxTw

iphone - 将 double 限制为两位小数,不带尾随零

我读过this和that.我正是想要这个:1.4324=>"1.43"9.4000=>"9.4"43.000=>"43"9.4=>"9.40"(wrong)43.000=>"43.00"(wrong)这两个问题的答案都指向NSNumberFormatter。所以它应该很容易实现,但对我来说不是。-(void)viewDidLoad{[superviewDidLoad];UILabel*myLabel=[[UILabelalloc]initWithFrame:CGRectMake(50,100,200,20)];NSNumberFormatter*doubleValueWithMaxTw

java - 如何在android中格式化longs以始终显示两位数

我有一个倒数计时器,它显示从60到0的秒数(1分钟倒数计时器)。当它达到1位数字时,例如9,8,7..它显示9而不是09。我尝试使用String.format("%[B]02d[/B]",x);我将x从long转换为string。没用。我想要String.format("%2d",1) 最佳答案 您可以使用DecimalFormat来完成它:NumberFormatf=newDecimalFormat("00");longtime=9;textView.setText(f.format(time));输出:09或者您可以使用Stri

java - 如何在android中格式化longs以始终显示两位数

我有一个倒数计时器,它显示从60到0的秒数(1分钟倒数计时器)。当它达到1位数字时,例如9,8,7..它显示9而不是09。我尝试使用String.format("%[B]02d[/B]",x);我将x从long转换为string。没用。我想要String.format("%2d",1) 最佳答案 您可以使用DecimalFormat来完成它:NumberFormatf=newDecimalFormat("00");longtime=9;textView.setText(f.format(time));输出:09或者您可以使用Stri

c# - 使用c#的两个小数位

decimalDebitvalue=1156.547m;decimalDEBITAMT=Convert.ToDecimal(string.Format("{0:0.00}",Debitvalue));我只需要得到两位小数,但通过使用此代码,我得到1156.547。让我知道我必须使用哪种格式来显示两位小数。 最佳答案 您的问题是要求显示两位小数。使用以下String.format会有所帮助:String.Format("{0:.##}",Debitvalue)这将显示最多两位小数的数字(例如2.10将显示为2.1)。如果您希望始终显示