Java中double保留兩位小數(shù)的多種方法
方法一:格式化輸出
double one=5;
System.out.printf("%.02f", one); //5.00方法二:BigDecimal.setScale()
BigDecimal.setScale()這個(gè)方法是用于格式化小數(shù)點(diǎn)
setScale(0)表示保留整數(shù)
setScale(1)表示保留一位小數(shù),默認(rèn)用四舍五入
setScale(1,BigDecimal.ROUND_DOWN)直接刪除多余的小數(shù)位,如2.35會(huì)變成2.3
setScale(1,BigDecimal.ROUND_UP)進(jìn)位處理,2.35變成2.4
setScale(1,BigDecimal.ROUND_HALF_UP)四舍五入,2.35變成2.4
setScale(1,BigDecimal.ROUND_HALF_DOWN)四舍五入,2.35變成2.3,如果是5則向下舍
BigDecimal num1 = new BigDecimal(2.225667); //這種寫法不允許,會(huì)造成精度損失
BigDecimal num = new BigDecimal(“2.225667”); //一般都會(huì)這樣寫最好
double one1 = 5.459; BigDecimal two1 = new BigDecimal(one1); double three1 = two1.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue(); System.out.println(three1); //5.46
方法三:DecimalFormat
double one2 = 5.459;
DecimalFormat format2 = new DecimalFormat("#.00");
String str2 = format2.format(one2);
System.out.println(str2); //5.46
double four2 = Double.parseDouble(str2);
System.out.println(four2); //5.46方法四:String.format
double one3 = 5.459;
String str3 = String.format("%.2f",one3);
System.out.println(str3); //5.46
double four3 = Double.parseDouble(str3);
System.out.println(four3); //5.46方法五:NumberFormat
double one4 = 5.459; NumberFormat format4 = NumberFormat.getInstance(); format4.setMaximumFractionDigits(2); String str4 = format4.format(one4); System.out.println(str4); //5.46 double two4 = Double.parseDouble(str4); System.out.println(two4); //5.46
補(bǔ)充:
- Double.valueOf(String s)返回保存用參數(shù)字符串s表示的double值的Double對(duì)象
- Double.parseDouble返回一個(gè)初始化為指定String表示的值的新double
- 如果要變成int,則用Integer.parseInt()==
方法二到四遇到的問題:
不能將double 5轉(zhuǎn)換成double 5.00輸出
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
public class test1 {
public static void main(String[] args) {
double one1 = 5;
BigDecimal two1 = new BigDecimal(one1);
double three1 = two1.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
System.out.println(three1); //5.0
double one2 = 5;
DecimalFormat format2 = new DecimalFormat("#.00");
String str2 = format2.format(one2);
System.out.println(str2); //5.00
double four2 = Double.parseDouble(str2);
System.out.println(four2); //5.0
double one3 = 5;
String str3 = String.format("%.2f",one3);
System.out.println(str3); //5.00
double four3 = Double.parseDouble(str3);
System.out.println(four3); //5.0
double one4 = 5;
NumberFormat format4 = NumberFormat.getInstance();
format4.setMaximumFractionDigits(2);
String str4 = format4.format(one4);
System.out.println(str4); //5
double two4 = Double.parseDouble(str4);
System.out.println(two4); //5.0
}
}
package code;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
public class test1 {
public static void main(String[] args) {
double one1 = 5.0;
BigDecimal two1 = new BigDecimal(one1);
double three1 = two1.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
System.out.println(three1); //5.0
double one2 = 5.0;
DecimalFormat format2 = new DecimalFormat("#.00");
String str2 = format2.format(one2);
System.out.println(str2); //5.00
double four2 = Double.parseDouble(str2);
System.out.println(four2); //5.0
double one3 = 5.0;
String str3 = String.format("%.2f",one3);
System.out.println(str3); //5.00
double four3 = Double.parseDouble(str3);
System.out.println(four3); //5.0
double one4 = 5.0;
NumberFormat format4 = NumberFormat.getInstance();
format4.setMaximumFractionDigits(2);
String str4 = format4.format(one4);
System.out.println(str4); //5
double two4 = Double.parseDouble(str4);
System.out.println(two4); //5.0
}
}
package code;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
public class test1 {
public static void main(String[] args) {
double one1 = 5.00;
BigDecimal two1 = new BigDecimal(one1);
double three1 = two1.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
System.out.println(three1); //5.0
double one2 = 5.00;
DecimalFormat format2 = new DecimalFormat("#.00");
String str2 = format2.format(one2);
System.out.println(str2); //5.00
double four2 = Double.parseDouble(str2);
System.out.println(four2); //5.0
double one3 = 5.00;
String str3 = String.format("%.2f",one3);
System.out.println(str3); //5.00
double four3 = Double.parseDouble(str3);
System.out.println(four3); //5.0
double one4 = 5.00;
NumberFormat format4 = NumberFormat.getInstance();
format4.setMaximumFractionDigits(2);
String str4 = format4.format(one4);
System.out.println(str4); //5
double two4 = Double.parseDouble(str4);
System.out.println(two4); //5.0
}
}
不管是5或者是5.000…最后的結(jié)果都是5.0
總結(jié)
到此這篇關(guān)于Java中double保留兩位小數(shù)的多種方法的文章就介紹到這了,更多相關(guān)Java double保留兩位小數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring Boot Admin Server管理客戶端過程詳解
這篇文章主要介紹了Spring Boot Admin Server管理客戶端過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03
java實(shí)現(xiàn)同步的幾種方式(示例詳解)
這篇文章主要介紹了java實(shí)現(xiàn)同步的幾種方式,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧2024-12-12
springboot整合mybatis-plus實(shí)現(xiàn)多表分頁查詢的示例代碼
這篇文章主要介紹了springboot整合mybatis-plus實(shí)現(xiàn)多表分頁查詢的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
Java深入了解數(shù)據(jù)結(jié)構(gòu)之棧與隊(duì)列的詳解
這篇文章主要介紹了Java數(shù)據(jù)結(jié)構(gòu)中的棧與隊(duì)列,在Java的時(shí)候,對(duì)于棧與隊(duì)列的應(yīng)用需要熟練的掌握,這樣才能夠確保Java學(xué)習(xí)時(shí)候能夠有扎實(shí)的基礎(chǔ)能力。本文小編就來詳細(xì)說說Java中的棧與隊(duì)列,需要的朋友可以參考一下2022-01-01
java數(shù)據(jù)結(jié)構(gòu)基礎(chǔ):單,雙向鏈表
這篇文章主要介紹了Java的數(shù)據(jù)解構(gòu)基礎(chǔ),希望對(duì)廣大的程序愛好者有所幫助,同時(shí)祝大家有一個(gè)好成績(jī),需要的朋友可以參考下,希望能給你帶來幫助2021-07-07
java正則表達(dá)式處理花括號(hào)內(nèi)容替換賦值問題
這篇文章主要介紹了java正則表達(dá)式處理花括號(hào)內(nèi)容替換賦值問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05
springcloud feign調(diào)其他微服務(wù)時(shí)參數(shù)是對(duì)象的問題
這篇文章主要介紹了springcloud feign調(diào)其他微服務(wù)時(shí)參數(shù)是對(duì)象的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03

