如何將BigDecimal對象轉換爲使用指數形式的字符串表示形式?如:3.134e67
?我查看了API,發現了toEngineeringString()
,但它並沒有給我我想要的。如何將BigDecimal轉換爲指數形式?
回答
你看了NumberFormat的文檔,這是從的DecimalFormat: 參見:http://docs.oracle.com/javase/1.4.2/docs/api/java/text/DecimalFormat.html
Scientific Notation
Numbers in scientific notation are expressed as the product of a mantissa and a power of ten, for example, 1234 can be expressed as 1.234 x 10^3. The mantissa is often in the range 1.0 <= x < 10.0, but it need not be. DecimalFormat can be instructed to format and parse scientific notation only via a pattern; there is currently no factory method that creates a scientific notation format. In a pattern, the exponent character immediately followed by one or more digit characters indicates scientific notation. Example: "0.###E0" formats the number 1234 as "1.234E3".
The number of digit characters after the exponent character gives the minimum exponent digit count. There is no maximum. Negative exponents are formatted using the localized minus sign, not the prefix and suffix from the pattern. This allows patterns such as "0.###E0 m/s".
The minimum and maximum number of integer digits are interpreted together:
If the maximum number of integer digits is greater than their minimum number and greater than 1, it forces the exponent to be a multiple of the maximum number of integer digits, and the minimum number of integer digits to be interpreted as 1. The most common use of this is to generate engineering notation, in which the exponent is a multiple of three, e.g., "##0.#####E0". Using this pattern, the number 12345 formats to "12.345E3", and 123456 formats to "123.456E3".
Otherwise, the minimum number of integer digits is achieved by adjusting the exponent. Example: 0.0formatted with "00.###E0" yields "12.3E-4".
The number of significant digits in the mantissa is the sum of the minimum integer and maximum fraction digits, and is unaffected by the maximum integer digits. For example, 12345 formatted with "##0.##E0" is "12.3E3". To show all digits, set the significant digits count to zero. The number of significant digits does not affect parsing.
Exponential patterns may not contain grouping separators.
你在找這樣的嗎?
YY^XX(MOD QQ)
int fastMod(int YY, int XX, int QQ){
int ZZ; //declare variables
int RR = 1;
while (XX != 0){ //while XX != 0
ZZ = XX % 2; //mod XX by 2
XX= XX/2; //divide XX by 2
if (ZZ == 1) //if ZZ is one
RR = (RR * YY) % QQ; //mod (RR*YY) by QQ
YY= (YY * YY) % QQ; //mod (YY*YY) by QQ
}
return RR; //return int
}
當你開始使用GIANT號碼,你將需要使用模運算。 當在RSA算法中生成大素數時,這變得特別有用。 模運算的基礎知識這裏介紹: http://www.brainjammer.com/math/modular-arithmetic/
我不明白。這如何幫助將BigDecimal轉換爲指數形式? – 2012-02-21 00:06:42
問題是,除非您使用模塊化算術,否則您將無法使用大數字。 – 2012-02-21 00:08:34
這是否幫助你嗎?
BigDecimal bd = new BigDecimal(3.134e67);
String.valueOf(bd.doubleValue())
- 1. 將BigDecimal轉換爲整數
- 2. java - 如何將Float轉換爲BigDecimal?
- 3. 如何將Apfloat轉換爲BigDecimal?
- 4. Excel數字轉換爲指數形式
- 5. EclipseLink將Enum轉換爲BigDecimal
- 6. 使用Gson將指數值轉換爲BigDecimal或Long
- 7. 爲什麼java將double轉換爲指數形式
- 8. Excel將數字轉換爲指數形式的文本
- 9. Flex將數字字符串轉換爲指數形式
- 10. 將數學公式轉換爲Java BigDecimal行
- 11. 如何將數學表達式樹轉換爲簡化形式?
- 12. 將數據轉換爲矩陣形式
- 13. 將JsonObject的值轉換爲BigDecimal
- 14. Jooq將字符串轉換爲BigDecimal
- 15. 將字符串轉換爲bigDecimal
- 16. 如何將指數形式的浮點值轉換爲點符號?
- 17. 將Bigdecimal轉換爲時間格式爲字符串
- 18. 將double轉換爲BigDecimal並設置BigDecimal Precision
- 19. 轉換字符串爲BigDecimal
- 20. 轉換數據,以指定的形式
- 21. 轉換到非指數形式
- 22. 如何在Perl中將數字轉換爲英文形式?
- 23. 如何將行中的數據轉換爲列形式?
- 24. 如何在clojure中將列表形式轉換爲函數
- 25. 如何將關聯數組轉換爲指定的格式?
- 26. 如何將Money轉換爲Java中的BigDecimal?
- 27. 如何將String空值轉換爲java中的BigDecimal
- 28. 如何將BigDecimal 0E-8轉換爲特定的雙精度值
- 29. 如何在Java中將BigDecimal轉換爲Double?
- 30. 如何將時間轉換爲ruby的bigdecimal?
我試過這個:'BigDecimal d = new BigDecimal(「55」); DecimalFormat df =新的DecimalFormat(「0。### E0」); System.out.println(df.format(d.toString()));',但它給了我一個錯誤:「無法格式化給定的對象作爲數字」 – 2012-02-21 00:18:58
它現在的作品,我剛剛取代'd.toString() '用'd'。謝謝 :) – 2012-02-21 00:27:43