Java實現(xiàn)控制小數(shù)精度的方法
生成double類型隨機數(shù)
random()函數(shù)源碼
/** * Creates a new random number generator. This constructor sets * the seed of the random number generator to a value very likely * to be distinct from any other invocation of this constructor. */ public Random() { this(seedUniquifier() ^ System.nanoTime()); }
nextDouble()函數(shù)源碼
public double nextDouble() { return (((long)(next(26)) << 27) + next(27)) * DOUBLE_UNIT; }
我們可以這樣生成一個doublel類型隨機數(shù)。
代碼
import java.util.Random; public class Format { public static void main(String[] args) { //方法1 Random random=new Random(); double num=random.nextDouble(); //方法2 //double num= Math.random(); System.out.println(num); } }
輸出:
0.04342853133845903
我們發(fā)現(xiàn)輸出結果是一個[0,1)之間的很長的小數(shù)值。如果我們不需要這么長的小數(shù)位數(shù)應該怎么處理呢?
控制小數(shù)位數(shù)
1.截斷 多余小數(shù)位
public class Format { public static void main(String[] args) { double d = 1.23456789; // 需要幾位小數(shù),就乘以10的幾次方,再強轉。 int i = (int) (d * 100000);//注意等式右邊帶了兩個() // 又轉回去。 double d2 = (double) i / 100000;//等式右邊必須加(double)并且i/10000不要加括號 System.out.println(d2); } }
輸出
1.23456
2.利用數(shù)字格式化
import java.text.NumberFormat; public class Format { public static void main(String[] args) { double d = 1.23456789; NumberFormat Nformat = NumberFormat.getInstance(); // 設置小數(shù)位數(shù)。 Nformat.setMaximumFractionDigits(2); // 對d進行轉換。 String str = Nformat.format(d); // 將String類型轉化位double //方法1 //Double num = Double.parseDouble(str); //方法2 double num=Double.valueOf(str).doubleValue(); System.out.println(num); } }
輸出:
1.23457
3.利用十進制格式化器
import java.text.DecimalFormat; public class Format { public static void main(String[] args) { double d = 1.23456789; // 設置格式樣式 DecimalFormat Dformat=new DecimalFormat("0.00000"); // 格式化 String str=Dformat.format(d); //將String類型轉化位double //Double num = Double.parseDouble(str);//方法1 double num=Double.valueOf(str).doubleValue();//方法2 System.out.println(num); } }
輸出
1.23457
4.利用BigDecimal(終極)
- BigDecimal是java.math包中提供的API類,可處理超過16位有效位的數(shù)。在開發(fā)中,如果我們需要精確計算的結果,則必須使用BigDecimal類來操作。
- BigDecimal所創(chuàng)建的是對象,故我們不能使用傳統(tǒng)的+、-、*、/等算術運算符直接對其對象進行數(shù)學運算,而必須調用其相對應的方法。方法中的參數(shù)也必須是BigDecimal的對象。構造器是類的特殊方法,專門用來創(chuàng)建對象,特別是帶有參數(shù)的對象。
import java.math.BigDecimal; public class Format { public static void main(String[] args) { double d = 1.23456789; BigDecimal decimal=new BigDecimal(d); // 四舍五入為五位小數(shù) double d2=decimal.setScale(5,BigDecimal.ROUND_HALF_UP).doubleValue(); System.out.println(d2); } }
輸出:
1.23457
參考資料:
Java控制小數(shù)位,獲得隨機數(shù)
BigDecimal詳解
Java字符串和數(shù)字間的轉換
到此這篇關于Java實現(xiàn)控制小數(shù)精度的方法的文章就介紹到這了,更多相關Java 小數(shù)精度內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
聊聊Java BigInteger里面的mod和remainder的區(qū)別
這篇文章主要介紹了聊聊Java BigInteger里面的mod和remainder的區(qū)別,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08Netty分布式抽象編碼器MessageToByteEncoder邏輯分析
這篇文章主要介紹了Netty分布式抽象編碼器MessageToByteEncoder的抽象邏輯分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-03-03解決springboot無法注入JpaRepository的問題
這篇文章主要介紹了解決springboot無法注入JpaRepository的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01java javax.annotation.Resource注解的詳解
這篇文章主要介紹了javax.annotation.Resource注解的詳解的相關資料,需要的朋友可以參考下2016-10-10SpringBoot異步調用方法實現(xiàn)場景代碼實例
這篇文章主要介紹了SpringBoot異步調用方法實現(xiàn)場景代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-04-04