久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

將 float 轉(zhuǎn)換為 double 而不會丟失精度

Convert float to double without losing precision(將 float 轉(zhuǎn)換為 double 而不會丟失精度)
本文介紹了將 float 轉(zhuǎn)換為 double 而不會丟失精度的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我有一個原始浮點數(shù),我需要一個原始雙精度數(shù).簡單地將浮點數(shù)轉(zhuǎn)換為 double 會給我?guī)砥婀值念~外精度.例如:

I have a primitive float and I need as a primitive double. Simply casting the float to double gives me weird extra precision. For example:

float temp = 14009.35F;
System.out.println(Float.toString(temp)); // Prints 14009.35
System.out.println(Double.toString((double)temp)); // Prints 14009.349609375

但是,如果我不進(jìn)行強制轉(zhuǎn)換,而是將浮點數(shù)輸出為字符串,并將字符串解析為雙精度,我會得到我想要的:

However, if instead of casting, I output the float as a string, and parse the string as a double, I get what I want:

System.out.println(Double.toString(Double.parseDouble(Float.toString(temp))));
// Prints 14009.35

有沒有更好的方法而不是去字符串然后返回?

Is there a better way than to go to String and back?

推薦答案

并不是你實際上獲得了額外的精度 - 而是浮點數(shù)沒有準(zhǔn)確地代表你的目標(biāo)數(shù)字起初.double is 準(zhǔn)確地代表了原來的浮點數(shù);toString 正在顯示已經(jīng)存在的額外"數(shù)據(jù).

It's not that you're actually getting extra precision - it's that the float didn't accurately represent the number you were aiming for originally. The double is representing the original float accurately; toString is showing the "extra" data which was already present.

例如(這些數(shù)字不正確,我只是在編造)假設(shè)你有:

For example (and these numbers aren't right, I'm just making things up) suppose you had:

float f = 0.1F;
double d = f;

那么 f 的值可能正好是 0.100000234523.d 將具有完全相同的值,但是當(dāng)您將其轉(zhuǎn)換為字符串時,它會相信"它的精確度更高,因此不會提前四舍五入,您會看到已經(jīng)存在但對您隱藏的額外數(shù)字".

Then the value of f might be exactly 0.100000234523. d will have exactly the same value, but when you convert it to a string it will "trust" that it's accurate to a higher precision, so won't round off as early, and you'll see the "extra digits" which were already there, but hidden from you.

當(dāng)您轉(zhuǎn)換為字符串并返回時,您最終會得到一個比原始浮點數(shù)更接近字符串值的雙精度值 - 但這只是 如果 你真的相信字符串值是您真正想要的.

When you convert to a string and back, you're ending up with a double value which is closer to the string value than the original float was - but that's only good if you really believe that the string value is what you really wanted.

您確定 float/double 是此處使用的合適類型,而不是 BigDecimal?如果您嘗試使用具有精確十進(jìn)制值的數(shù)字(例如貨幣),那么 BigDecimal 是更合適的 IMO 類型.

Are you sure that float/double are the appropriate types to use here instead of BigDecimal? If you're trying to use numbers which have precise decimal values (e.g. money), then BigDecimal is a more appropriate type IMO.

這篇關(guān)于將 float 轉(zhuǎn)換為 double 而不會丟失精度的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環(huán)繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數(shù)據(jù)庫)
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 青青草综合网 | 精品欧美乱码久久久久久1区2区 | 亚洲午夜电影 | 成人精品福利 | 男人天堂久久 | 欧美精品一区二区在线观看 | 国产精品久久久久久久免费观看 | 国产亚洲精品精品国产亚洲综合 | 91在线最新 | 久久蜜桃资源一区二区老牛 | 国产精品不卡一区二区三区 | 特级做a爰片毛片免费看108 | av毛片在线播放 | 国产欧美一区二区三区在线看 | 精品综合在线 | 一区二区免费在线 | 日韩成人在线免费视频 | 99久久久99久久国产片鸭王 | 福利视频网址 | 欧美日韩亚洲系列 | 日韩精品欧美精品 | 狠狠狠色丁香婷婷综合久久五月 | 在线观看免费毛片 | 亚洲性综合网 | 日韩视频中文字幕 | 亚洲激情综合网 | 亚洲天堂久久新 | 国产在线中文字幕 | 久久国产精品一区二区三区 | 精品欧美视频 | 日韩1区| 国产精品国产a | 婷婷久久一区 | 免费看黄视频网站 | 一区二区三区中文字幕 | 男人的天堂在线视频 | 这里只有精品999 | 日本黄色短片 | 色综合成人网 | 日韩一区二区在线视频 | 麻豆91av|