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類型。
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)類型 /**乘以多少就保留多少位小數(shù) **注意Math.round()方法傳float類型! */
兩種方法都會(huì)四舍五入。
如果是浮點(diǎn)類型建議用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類型數(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類型
System.out.println(Float.parseFloat(df1.format(f)));//轉(zhuǎn)換為float類型
System.out.println(String.valueOf(df1.format(f)));
// System.out.println(Float.toString(df1.format(f)));//轉(zhuǎn)換為String類型
f = 0.595f;
//Math.round()方法是將浮點(diǎn)類型數(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)文章
Java服務(wù)調(diào)用RestTemplate與HttpClient的使用詳解
無(wú)論是微服務(wù)還是SOA,都面臨著服務(wù)間的遠(yuǎn)程調(diào)用,這篇文章主要介紹了服務(wù)調(diào)用RestTemplate與HttpClient的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
Springmvc自定義參數(shù)轉(zhuǎn)換實(shí)現(xiàn)代碼解析
這篇文章主要介紹了Springmvc自定義參數(shù)轉(zhuǎn)換實(shí)現(xiàn)代碼解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07
SpringSecurity實(shí)現(xiàn)權(quán)限認(rèn)證與授權(quán)的使用示例
本文主要介紹了SpringSecurity實(shí)現(xiàn)權(quán)限認(rèn)證與授權(quán)的使用示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-11-11
Spring?Boot中的max-http-header-size配置方式
這篇文章主要介紹了Spring?Boot中的max-http-header-size配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09
Java中的 FilterInputStream簡(jiǎn)介_(kāi)動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
FilterInputStream 的作用是用來(lái)“封裝其它的輸入流,并為它們提供額外的功能”。接下來(lái)通過(guò)本文給大家分享Java中的 FilterInputStream簡(jiǎn)介,感興趣的朋友一起學(xué)習(xí)吧2017-05-05
spring boot activiti工作流的搭建與簡(jiǎn)單使用
這篇文章主要給大家介紹了關(guān)于spring boot activiti工作流的搭建與簡(jiǎn)單使用的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-08-08
詳解Java8?CompletableFuture的并行處理用法
Java8中有一個(gè)工具非常有用,那就是CompletableFuture,本章主要講解CompletableFuture的并行處理用法,感興趣的小伙伴可以了解一下2022-04-04

