「Java」「四捨五入」

轉載請註明,原文地址:

<code>http:

//www

.lgygg.wang/lgyblog/

2020

/

02

/

19

/java%e5%9b%9b%e8%88%8d%e4%ba%94%e5%85%a5/ /<code>

1.方法一 String.format和DecimalFormat

<code>String tmp = String.format(

"%.2f"

, x); String tmp2 = String.format(

"%.2f"

, y); System.

out

.println(tmp+

" "

+tmp2);

Double

d =

Double

.valueOf(tmp); System.

out

.println(

"方法一 Double.valueOf(tmp):"

+d);

Double

d2 =

Double

.parseDouble(tmp); System.

out

.println(

"方法一 Double.parseDouble(tmp):"

+d2); DecimalFormat df = new DecimalFormat(

"#.##"

); System.

out

.println(

"方法一 df.format(x):"

+df.format(x)); System.

out

.println(

"方法一 df.format(z):"

+df.format(z)); DecimalFormat df2 = new DecimalFormat(

"#0.00"

); System.

out

.println(

"方法一 df2.format(x):"

+df2.format(x)); System.

out

.println(

"方法一 df2.format(z):"

+df2.format(z)); /<code>

總結:(1)可以使用String的format()來保存小數點後幾位,但是String.format()方法返回的結果是一個String,所以如果你想得到的是一個double,那麼 你還需要使用Double.valueOf或Double.parseDouble來轉換(2)額外說一下Double.valueOf和Double.parseDouble的區別,它們的區別就是返回值的類型不一樣,Double.valueOf返回的是Double(對象類型),而Double.parseDouble返回的是double(基礎類型)


(3)還可以通過DecimalFormat來進行四捨五入,但它其實和String.format的作用差不多,只不過format的格式不大一樣
String.format(“%.2f”, x)會保留兩位小數,最後一位如果是0也會顯示
DecimalFormat要使用new DecimalFormat(“0.00”)保留兩位小數,最後一位如果是0也會顯示


2.方法二 Math.round

<code>System.

out

.println(

"方法二 (Math.round(x)*100)/100:"

+(Math.round(x*

100

))/

100.0f

); System.

out

.println(

"方法二 (Math.round(y)*100)/100:"

+(Math.round(y*

100

))/

100.0f

); System.

out

.println(

"方法二 (Math.round(z)*100)/100:"

+(Math.round(z*

100

))/

100.0f

); /<code>

總結:其實說到四捨五入我第一個想到的就是Math.round()方法,但是Math.round()返回的結果是一個整數,沒有設置保留小數點後幾位的方法,但是也可以先將它小數的後兩位變成整數,再除以100.0f(不能除以100,否則返回的是整數)即可,但是如果希望結果是3.20,那麼還是需要使用format來進行格式化。總之Math.round()進行四捨五入比較麻煩,不推薦使用。

3.方法三 BigDecimal

以下是來自百度百科對BigDecimal的介紹。Java在java.math包中提供的API類BigDecimal,用來對超過16位有效位的數進行精確的運算。雙精度浮點型變量double可以處理16位有效數。在實際應用中,需要對更大或者更小的數進行運算和處理。


float和double只能用來做科學計算或者是工程計算,在商業計算中要用java.math.BigDecimal。
BigDecimal所創建的是對象,我們不能使用傳統的+、-、*、/等算術運算符直接對其對象進行數學運算,而必須調用其相對應的方法。


方法中的參數也必須是BigDecimal的對象。構造器是類的特殊方法,專門用來創建對象,特別是帶有參數的對象。


<code> 

BigDecimal bd = 

new

BigDecimal(x); BigDecimal bs = bd.setScale(

2

, BigDecimal.ROUND_HALF_UP); System.

out

.println(

"方法三 bs:"

+bs); BigDecimal bd2 =

new

BigDecimal(y); BigDecimal bs2 = bd2.setScale(

2

, BigDecimal.ROUND_HALF_UP); System.

out

.println(

"方法三 bs2:"

+bs2); BigDecimal bd3 =

new

BigDecimal(z); BigDecimal bs3 = bd3.setScale(

2

, BigDecimal.ROUND_HALF_UP); System.

out

.println(

"方法三 bs3:"

+bs3); /<code>

總結:BigDecimal很方便進行四捨五入,但是由於它是用來對超過16位有效位的數進行精確的運算,也意味著佔用的內存比double多

4.方法四 NumberFormat

<code>NumberFormat nf = NumberFormat.getNumberInstance();
 
nf.setMaximumFractionDigits(

2

); nf.setRoundingMode(RoundingMode.HALF_UP); System.

out

.println(

"方法三 nf.format(x):"

+nf.format(x)); System.

out

.println(

"方法三 nf.format(y):"

+nf.format(y)); System.

out

.println(

"方法三 nf.format(z):"

+nf.format(z)); /<code>

總結:NumberFormat也比較方便,但是返回的也是StringNumberFormat 是所有數值格式的抽象基類。此類提供格式化和解析數值的接口。

5.源碼

<code>package com.lgy.test;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.NumberFormat;

 

public

class

Test

{

public

static

void

main

(

String[] args

)

{

float

x =

3.1984f

;

double

y =

3.142840395056

;

float

z =

0.1984f

; String tmp = String.format(

"%.2f"

, x); String tmp2 = String.format(

"%.2f"

, y); System.

out

.println(tmp+

" "

+tmp2); Double d = Double.valueOf(tmp); System.

out

.println(

"方法一 Double.valueOf(tmp):"

+d); Double d2 = Double.parseDouble(tmp); System.

out

.println(

"方法一 Double.parseDouble(tmp):"

+d2); DecimalFormat df =

new

DecimalFormat(

"#.##"

); System.

out

.println(

"方法一 df.format(x):"

+df.format(x)); System.

out

.println(

"方法一 df.format(z):"

+df.format(z)); DecimalFormat df2 =

new

DecimalFormat(

"#0.00"

); System.

out

.println(

"方法一 df2.format(x):"

+df2.format(x)); System.

out

.println(

"方法一 df2.format(z):"

+df2.format(z)); System.

out

.println(

"======================================="

); System.

out

.println(

"方法二 (Math.round(x)*100)/100:"

+(Math.round(x*

100

))/

100.0f

); System.

out

.println(

"方法二 (Math.round(y)*100)/100:"

+(Math.round(y*

100

))/

100.0f

); System.

out

.println(

"方法二 (Math.round(z)*100)/100:"

+(Math.round(z*

100

))/

100.0f

); System.

out

.println(

"======================================="

); BigDecimal bd =

new

BigDecimal(x); BigDecimal bs = bd.setScale(

2

, BigDecimal.ROUND_HALF_UP); System.

out

.println(

"方法三 bs:"

+bs); BigDecimal bd2 =

new

BigDecimal(y); BigDecimal bs2 = bd2.setScale(

2

, BigDecimal.ROUND_HALF_UP); System.

out

.println(

"方法三 bs2:"

+bs2); BigDecimal bd3 =

new

BigDecimal(z); BigDecimal bs3 = bd3.setScale(

2

, BigDecimal.ROUND_HALF_UP); System.

out

.println(

"方法三 bs3:"

+bs3); System.

out

.println(

"======================================="

); NumberFormat nf = NumberFormat.getNumberInstance(); nf.setMaximumFractionDigits(

2

); nf.setRoundingMode(RoundingMode.HALF_UP); System.

out

.println(

"方法三 nf.format(x):"

+nf.format(x)); System.

out

.println(

"方法三 nf.format(y):"

+nf.format(y)); System.

out

.println(

"方法三 nf.format(z):"

+nf.format(z)); } }/<code>


「Java」「四捨五入」


分享到:


相關文章: