Java?Float?保留小數(shù)位精度的實(shí)現(xiàn)
Float 保留小數(shù)位精度
DecimalFormat decimalFormat=new DecimalFormat(".00");
return Float.valueOf(super.getDecimalFormat().format(new BigDecimal(handleTime)));
Float 浮點(diǎn)型數(shù)據(jù)保留兩位小數(shù)
用過(guò)兩種方法:DecimalFormat和Math.round()。
用法:
1、DecimalFormat
String java.text.NumberFormat.format(double number)方法
float f = 0.5555f;
DecimalFormat df1 = new DecimalFormat("#.00");//保留兩位小數(shù),如果是零點(diǎn)幾,則小數(shù)點(diǎn)前的0不顯示,小數(shù)點(diǎn)后幾個(gè)零就保留幾位
df1.format(f);
#表示該位如果是0則不必顯示出來(lái),0則表示該位如果是0仍然顯示;
函數(shù)的定義是:;
所以,傳參是double,也可以傳float(隱式轉(zhuǎn)換),最后的結(jié)果是String類(lèi)型。
2、Math.round()
int java.lang.Math.round(float a)方法
float f = 1.222f; f = Math.round(f * 100) / 100f;//乘以100,然后除以100轉(zhuǎn)換為浮點(diǎn)類(lèi)型 /**乘以多少就保留多少位小數(shù) **注意Math.round()方法傳float類(lèi)型! */
兩種方法都會(huì)四舍五入。
如果是浮點(diǎn)類(lèi)型建議用Math.round方法,也可以根據(jù)自己需求diy代碼。
詳細(xì)講解請(qǐng)看代碼注釋和控制臺(tái)輸出:(包含decimalFloat的格式、Math.round函數(shù)的實(shí)現(xiàn)邏輯)
package testMap;
import java.text.DecimalFormat;
public class TestFloat {
public static void main(String[] args) {
// TODO Auto-generated method stub
//四舍五入保留兩位
float f = 0.5555f;
//decimalFormat是將double類(lèi)型數(shù)據(jù)轉(zhuǎn)換為字符串,并進(jìn)行格式化
//#表示這位如果是0則不必顯示出來(lái),0則表示這位如果是
//format函數(shù):String java.text.NumberFormat.format(double number)
DecimalFormat df1 = new DecimalFormat("#.00");//首位0不顯示出來(lái),即0.1顯示為 .1
DecimalFormat df2 = new DecimalFormat("0.00");//首位0顯示出來(lái),即0.1顯示為 0.1
System.out.println("--------DecimalFormat----------");
System.out.println("df1==" + df1.format(f));
System.out.println("df2==" + df2.format(f));
System.out.println(df1.format(f).getClass());//String類(lèi)型
System.out.println(Float.parseFloat(df1.format(f)));//轉(zhuǎn)換為float類(lèi)型
System.out.println(String.valueOf(df1.format(f)));
// System.out.println(Float.toString(df1.format(f)));//轉(zhuǎn)換為String類(lèi)型
f = 0.595f;
//Math.round()方法是將浮點(diǎn)類(lèi)型數(shù)據(jù) 乘以10的多少次方 ,取整,然后 + 0.5 除以10的多少次方,取小數(shù)點(diǎn)后多少位
// 如乘以1000 則取小數(shù)點(diǎn)后3位
System.out.println("---------Math.round()----------");
System.out.println(Math.round(f * 100) / 100f);//四舍五入后如果末尾是0,自動(dòng)省略,不顯示
// System.out.println(df1.format("1.2"));//參數(shù)必須是數(shù)值型String java.text.NumberFormat.format(double number)
System.out.println(Float.toString(f));//轉(zhuǎn)換為String輸出效果
System.out.println(Float.toString(f));//轉(zhuǎn)換為String輸出效果
System.out.println("-----------Math.round()的正數(shù)非特殊值實(shí)現(xiàn)邏輯--------------");
f = 11.115111f;
int b = (int) (f * 100 + 0.5);
float a = b / 100f;
System.out.println("a==" + a);
System.out.println((int)(f * 100 + 0.5) / 100f);
f = -12.115f;
System.out.println("負(fù)數(shù)" + Math.round(f * 100) / 100f);
f = -12.116f;
System.out.println("負(fù)數(shù)" + Math.round(f * 100) / 100f);
System.out.println("-------Math.round()的負(fù)數(shù)非特殊值實(shí)現(xiàn)邏輯--------");
int c = (int) (f * 100 - 0.5);
float d = c / 100f;
System.out.println("d==" + d);
System.out.println((int)(d * 100 - 0.5) / 100f);
}
}
控制臺(tái)輸出:
截圖如下:

----下面的是控制臺(tái)輸出-----(和上面一樣的,怕圖片丟失)
--------DecimalFormat----------
df1==.56
df2==0.56
class java.lang.String
0.56
.56
---------Math.round()----------
0.6
0.595
0.595
-----------Math.round()的正數(shù)非特殊值實(shí)現(xiàn)邏輯--------------
a==11.12
11.12
負(fù)數(shù)-12.11
負(fù)數(shù)-12.12
-------Math.round()的負(fù)數(shù)非特殊值實(shí)現(xiàn)邏輯--------
d==-12.12
-12.12
順便貼上NumberFormat.formart()的代碼:
/**
* Specialization of format.
*
* @param number the double number to format
* @return the formatted String
* @exception ArithmeticException if rounding is needed with rounding
* mode being set to RoundingMode.UNNECESSARY
* @see java.text.Format#format
*/
public final String format(double number) {
// Use fast-path for double result if that works
String result = fastFormat(number);
if (result != null)
return result;
return format(number, new StringBuffer(),
DontCareFieldPosition.INSTANCE).toString();
}
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
如何在Maven項(xiàng)目配置pom.xml指定JDK版本和編碼
maven是個(gè)項(xiàng)目管理工具,如果我們不告訴它要使用什么樣的jdk版本編譯,它就會(huì)用maven-compiler-plugin默認(rèn)的jdk版本來(lái)處理,這樣就容易出現(xiàn)版本不匹配的問(wèn)題,這篇文章主要給大家介紹了關(guān)于如何在Maven項(xiàng)目配置pom.xml指定JDK版本和編碼的相關(guān)資料,需要的朋友可以參考下2024-01-01
Spring Mvc中傳遞參數(shù)方法之url/requestMapping詳解
在開(kāi)發(fā)中,參數(shù)傳遞是必不可少的一個(gè)功能,下面這篇文章主要給大家介紹了關(guān)于Spring Mvc中傳遞參數(shù)方法之url/requestMapping的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-07-07
Spring前后端跨域請(qǐng)求設(shè)置代碼實(shí)例
這篇文章主要介紹了Spring前后端跨域請(qǐng)求設(shè)置代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07
idea2019.2安裝MybatisCodeHelper插件的超詳細(xì)教程
這篇文章主要介紹了idea2019.2安裝MybatisCodeHelper插件的教程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
簡(jiǎn)單了解Spring Cloud搭建Config過(guò)程實(shí)例
這篇文章主要介紹了簡(jiǎn)單了解Spring Cloud搭建Config過(guò)程實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12
到底如何設(shè)置Java線(xiàn)程池的大小的方法示例
在我們?nèi)粘I(yè)務(wù)開(kāi)發(fā)過(guò)程中,或多或少都會(huì)用到并發(fā)的功能。那么并發(fā)線(xiàn)程池到底設(shè)置多大呢?文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
Spring Boot與RabbitMQ結(jié)合實(shí)現(xiàn)延遲隊(duì)列的示例
本篇文章主要介紹了Spring Boot與RabbitMQ結(jié)合實(shí)現(xiàn)延遲隊(duì)列的示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11

