![](/img/trans.png)
[英]How do I format a double into a string (with only two decimal places)?
[英]How can I truncate a double to only two decimal places in Java?
例如,我有變量 3.545555555,我想將其截斷為 3.54。
如果您希望將其用於顯示目的,請使用java.text.DecimalFormat
:
new DecimalFormat("#.##").format(dblVar);
如果您需要它進行計算,請使用java.lang.Math
:
Math.floor(value * 100) / 100;
DecimalFormat df = new DecimalFormat(fmt);
df.setRoundingMode(RoundingMode.DOWN);
s = df.format(d);
檢查可用的RoundingMode
和DecimalFormat
。
其他答案都不適用於正值和負值(我的意思是計算,只是為了“截斷”而不進行舍入)。 並且不轉換為字符串。
private static BigDecimal truncateDecimal(double x,int numberofDecimals)
{
if ( x > 0) {
return new BigDecimal(String.valueOf(x)).setScale(numberofDecimals, BigDecimal.ROUND_FLOOR);
} else {
return new BigDecimal(String.valueOf(x)).setScale(numberofDecimals, BigDecimal.ROUND_CEILING);
}
}
這種方法對我來說效果很好。
System.out.println(truncateDecimal(0, 2));
System.out.println(truncateDecimal(9.62, 2));
System.out.println(truncateDecimal(9.621, 2));
System.out.println(truncateDecimal(9.629, 2));
System.out.println(truncateDecimal(9.625, 2));
System.out.println(truncateDecimal(9.999, 2));
System.out.println(truncateDecimal(-9.999, 2));
System.out.println(truncateDecimal(-9.0, 2));
結果 :
0.00
9.62
9.62
9.62
9.62
9.99
-9.99
-9.00
首先請注意, double
精度數是二進制分數,實際上沒有小數位。
如果您需要小數位,請使用BigDecimal
,它具有用於截斷的setScale()
方法,或使用DecimalFormat
獲取String
。
如果出於某種原因,您不想使用BigDecimal
,您可以將您的double
轉換為int
以截斷它。
如果你想截斷到Ones 的地方:
int
第十名:
int
double
百位
例子:
static double truncateTo( double unroundedNumber, int decimalPlaces ){
int truncatedNumberInt = (int)( unroundedNumber * Math.pow( 10, decimalPlaces ) );
double truncatedNumber = (double)( truncatedNumberInt / Math.pow( 10, decimalPlaces ) );
return truncatedNumber;
}
在此示例中, decimalPlaces
將是 PAST 您希望去的地方的位數,因此 1 將四舍五入到十分位,2 到百分之一,依此類推(0 舍入到個位,負一到十等)
格式化為字符串並轉換回雙精度我認為會給你你想要的結果。
雙精度值不會是 round()、floor() 或 ceil()。
一個快速的解決方法可能是:
String sValue = (String) String.format("%.2f", oldValue);
Double newValue = Double.parseDouble(sValue);
您可以使用 sValue 進行顯示或使用 newValue 進行計算。
您可以使用 NumberFormat 類對象來完成任務。
// Creating number format object to set 2 places after decimal point
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(2);
nf.setGroupingUsed(false);
System.out.println(nf.format(precision));// Assuming precision is a double type variable
3.545555555 得到 3.54。 嘗試關注這個:
DecimalFormat df = new DecimalFormat("#.##");
df.setRoundingMode(RoundingMode.FLOOR);
double result = new Double(df.format(3.545555555);
這將給出= 3.54!
也許Math.floor(value * 100) / 100
? 請注意,像3.54
這樣的值可能無法用double
精確表示。
這是我使用的方法:
double a=3.545555555; // just assigning your decimal to a variable
a=a*100; // this sets a to 354.555555
a=Math.floor(a); // this sets a to 354
a=a/100; // this sets a to 3.54 and thus removing all your 5's
這也可以做到:
a=Math.floor(a*100) / 100;
快速檢查是使用 Math.floor 方法。 我創建了一種方法來檢查以下兩位或更少小數位的雙精度數:
public boolean checkTwoDecimalPlaces(double valueToCheck) {
// Get two decimal value of input valueToCheck
double twoDecimalValue = Math.floor(valueToCheck * 100) / 100;
// Return true if the twoDecimalValue is the same as valueToCheck else return false
return twoDecimalValue == valueToCheck;
}
double value = 3.4555;
String value1 = String.format("% .3f", value) ;
String value2 = value1.substring(0, value1.length() - 1);
System.out.println(value2);
double doublevalue= Double.valueOf(value2);
System.out.println(doublevalue);
我使用 Math.floor() 方法和基本的小數位移動 (100 = 2)。
//3.545555555 to 3.54 by floor method
double x = 3.545555555;
double y = Math.floor(x * 100); //354
double z = y / 100; //3.54
雙首值 = -3.1756d;
雙值1 = (((int)(Math.pow(10,3)*firstValue))/Math.pow(10,3));
在此解決方案中,這會將雙精度數截斷到小數點后兩位。 此解決方案不會舍入雙精度值。
double myDoubleNumber = 3.545555555;
DecimalFormat df = new DecimalFormat("#.##");
df.setRoundingMode(RoundingMode.DOWN);
double myDoubleNumberTruncated = Double.parseDouble(df.format(myDoubleNumber));
System.out.println(myDoubleNumberTruncated);
這將輸出3.54
DecimalFormat("#.##") - 在這里,我在小數點后輸入了兩個井號 (##)。 因此,這會將數字截斷至小數點后兩位。
這適用於正值和負值。
也許如下:
double roundTwoDecimals(double d) {
DecimalFormat twoDForm = new DecimalFormat("#.##");
return Double.valueOf(twoDForm.format(d));
}
我有一個稍微修改過的Mani版本。
private static BigDecimal truncateDecimal(final double x, final int numberofDecimals) {
return new BigDecimal(String.valueOf(x)).setScale(numberofDecimals, BigDecimal.ROUND_DOWN);
}
public static void main(String[] args) {
System.out.println(truncateDecimal(0, 2));
System.out.println(truncateDecimal(9.62, 2));
System.out.println(truncateDecimal(9.621, 2));
System.out.println(truncateDecimal(9.629, 2));
System.out.println(truncateDecimal(9.625, 2));
System.out.println(truncateDecimal(9.999, 2));
System.out.println(truncateDecimal(3.545555555, 2));
System.out.println(truncateDecimal(9.0, 2));
System.out.println(truncateDecimal(-9.62, 2));
System.out.println(truncateDecimal(-9.621, 2));
System.out.println(truncateDecimal(-9.629, 2));
System.out.println(truncateDecimal(-9.625, 2));
System.out.println(truncateDecimal(-9.999, 2));
System.out.println(truncateDecimal(-9.0, 2));
System.out.println(truncateDecimal(-3.545555555, 2));
}
輸出:
0.00
9.62
9.62
9.62
9.62
9.99
9.00
3.54
-9.62
-9.62
-9.62
-9.62
-9.99
-9.00
-3.54
這對我有用:
double input = 104.8695412 //For example
long roundedInt = Math.round(input * 100);
double result = (double) roundedInt/100;
//result == 104.87
我個人喜歡這個版本,因為它實際上以數字方式執行舍入,而不是通過將其轉換為字符串(或類似的)然后對其進行格式化。
例如,我有變量3.545555555,我希望將其截斷為3.54。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.