3
A
回答
3
這個答案使用十進制格式化。它假設輸入數字總是嚴格的形式( - )xx.yyy。
/**
* Converts a double of the form xx.yyy to xxyyy and -xx.yyy to -xxyy.
* No rounding is performed.
*
* @param number The double to format
* @return The formatted number string
*/
public static String format(double number){
DecimalFormat formatter = new DecimalFormat("#");
formatter.setRoundingMode(RoundingMode.DOWN);
number *= number < 0.0 ? 100 : 1000;
String result = formatter.format(number);
return result;
}
8
double yourDouble = 61.9155;
String str = String.valueOf(yourDouble).replace(".", "");
說明:
String.valueOf()
:轉換你的雙成Stringstr.replace(s1, s2)
:返回一個新字符串等於str
所有s1
小號
s2
取代'
更新:
的OP有一些額外的條件(但我不正好與一個知道):
負數 - >只有兩位小數。
public static String doubleToSpecialString(double d) { if (d >= 0) { return String.valueOf(d).replace(".", ""); } else { return String.format("%.2f", d).replace(",", ""); } }
負數 - >一個小數較少
public static String doubleToSpecialString(double d) { if (d >= 0) { return String.valueOf(d).replace(".", ""); } else { String str = String.valueOf(d); int dotIndex = str.indexOf("."); int decimals = str.length() - dotIndex - 1; return String.format("%." + (decimals - 1) + "f", d).replace(",", ""); } }
相關問題
- 1. System.InvalidCastException Double to String
- 2. VB.Net String to double
- 3. Parse String to double []
- 4. Custom Formatting Double
- 5. String to double,precision lost
- 6. Buggy String Array to Double Array Conversion
- 7. java to excel date formatting
- 8. Parsed String of Dates not formatting facelet
- 9. SQL Server CE演變/轉換Double to String
- 10. double + double = String?
- 11. Convert double [] to long []
- 12. Casting object [,] to string []
- 13. sscanf()double&string
- 14. NSNumberFormatter:string to Double
- 15. reinterpret_cast double to char *
- 16. Json to Double type JAVA
- 17. char to string error
- 18. Java String to unicode
- 19. Base64 String to Json String
- 20. ArrayBuffer to String,String to ArrayBuffer方法
- 21. HashTable <String Double>
- 22. unsigned long long to double
- 23. Round double to top - C#
- 24. rich:listShuttle formatting
- 25. 將RDD [Map [String,Double]]轉換爲RDD [(String,Double)]
- 26. double.MaxValue to string is unsverted back
- 27. base 64 string to hexa string
- 28. std :: string xml string to object
- 29. Angular2:Subject <string> to string
- 30. c#string [] to jquery string list?
使用DecimalFormat而不是toString的+1使用 – 2010-09-09 17:47:23
沒錯!有用... – mtz 2010-09-10 07:35:38