如何将双倍小数舍入到小数点后一位? [英] How do I round a double to one decimal place?
本文介绍了如何将双倍小数舍入到小数点后一位?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想以#格式给我一个随机的双重回复。#然而我得到了这个效果的值:0.9395772067578356
I am trying to get a random double returned to me in the format #.# however I am getting a value of something to this effect: 0.9395772067578356
怎么做因为我不能在.nextDouble中添加参数,你只强制在随机双精度上输入一个小数。
How do you force only a one decimal return on a random double as I cannot put paraments in the .nextDouble.
myRandomNumGenerator = new Random();
loadedValue = myRandomNumGenerator.nextDouble();
推荐答案
DecimalFormat oneDigit = new DecimalFormat("#,##0.0");//format to 1 decimal place
System.out.println(oneDigit.format(anyVariable)); //generic usage
loadedValue = Double.valueOf(oneDigit.format(loadedValue));//specific usage posted in comments
这篇关于如何将双倍小数舍入到小数点后一位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文