0
A
回答
0
你的問題不是真的完整,如果你想顯示十進制數的整數值(向上/向下取整),那麼你只需要* 10000然後將它舍入,然後格式化爲RC回答。
public static void main(String[] args) {
float original = 0.2f;
float multiplied = original *10000;
int intvalue = (int)multiplied; // you can change to round up/down
String d = String.format("%04d", intvalue);
System.out.println("Value d : "+d);
}
檢查這裏的格式https://dzone.com/articles/java-string-format-examples
0
這是假設所需的輸出是2000的解決方案:
System.out.println(String.format("%.4f", 0.2).replaceFirst("^0\\.", ""));
同爲0000:
System.out.println(String.format("%.4f", 0.0).replaceFirst("^0\\.", ""));
您可以使用以下方法:
private static String convert(double v) {
return String.format("%.4f", v).replaceFirst("^0\\.", "");
}
+0
我需要從0.2和0000輸出2000以從0輸出 –
0
我錯在先。這裏是使用DecimalFormatter的正確答案,它適合你的情況。假設您已經定義double d;
DecimalFormat formatter = new DecimalFormat("#0000");
System.out.println(formatter.format(d * 10000));
輸出將被格式化爲使用#0000
4位。
0 -> 0000
0.2 -> 2000
0.8 -> 8000
相關問題
- 1. 從UITextView追加格式化的字符串到已格式化的字符串?
- 2. 插入格式化字符串到StringBuilder
- 3. 枚舉到格式化字符串
- 4. 格式化日期到字符串
- 5. 字符串格式化
- 6. groovy字符串格式化
- 7. TimeZone字符串格式化
- 8. 格式化的字符串
- 9. 格式化字符串
- 10. 格式化字符串2.7
- 11. 格式化字符串
- 12. 格式化字符串
- 13. PHP - 格式化字符串
- 14. 字符串格式化django
- 15. 格式化字符串
- 16. 格式化字符串「\」
- 17. 格式化C字符串
- 18. Python字符串格式化
- 19. 格式化的字符串
- 20. JS字符串格式化
- 21. C#+字符串格式化
- 22. PHP字符串格式化
- 23. MySQL:格式化字符串
- 24. iPhone格式化字符串
- 25. 字符串格式化
- 26. 格式化Java字符串
- 27. 字符串格式化
- 28. 如何使用本地化字符串格式化字符串?
- 29. 使用字符串模式格式化字符串
- 30. Log4j隱式字符串格式化
'* 10000'和'String.format' – 2017-10-12 10:46:26
只是爲了我的理解:你想要打印0.2000? –