亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

[GWT][Java] 科學記數法自動舍入?

[GWT][Java] 科學記數法自動舍入?

慕妹3146593 2022-06-04 15:27:31
我需要使用科學記數法來表示一個值。我使用此代碼,但值是四舍五入的,我不希望這樣......import com.google.gwt.i18n.client.NumberFormat;...Double value = Double.parseDouble("0.00000012");String formatted = NumberFormat.getScientificFormat().format(value);System.out.println(formatted);結果是:1E-7并不是1.2E-7你可以幫幫我嗎?謝謝 :)
查看完整描述

3 回答

?
慕俠2389804

TA貢獻1719條經驗 獲得超6個贊

您可以通過標準實現您想要的java.text.DecimalFormat


import java.text.DecimalFormat;

...


Double value = Double.parseDouble("0.00000012");

DecimalFormat formatter = new DecimalFormat("0.#####E0");

String formatted = formatter.format(value);

System.out.println(formatted);

這打印出來:1.2E-7


查看完整回答
反對 回復 2022-06-04
?
慕尼黑8549860

TA貢獻1818條經驗 獲得超11個贊

使用您的 GWT NumberFormat,您可以嘗試以下方法,看看它是否有效?(例如使用 BigDecimal 而不是 Double)

System.out.println(NumberFormat.getScientificFormat().format(new BigDecimal("0.00000012")));



查看完整回答
反對 回復 2022-06-04
?
RISEBY

TA貢獻1856條經驗 獲得超5個贊

funkyjelly 的答案中使用的相同格式模式應該適用于 GWT 的NumberFormat類型:


import com.google.gwt.i18n.client.NumberFormat;

...

Double value = Double.parseDouble("0.00000012");

String formatted = NumberFormat.getFormat("0.#####E0").format(value);

System.out.println(formatted);


查看完整回答
反對 回復 2022-06-04
  • 3 回答
  • 0 關注
  • 115 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號