Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java中格式化此字符串?_Java_String - Fatal编程技术网

如何在java中格式化此字符串?

如何在java中格式化此字符串?,java,string,Java,String,如何在Java中格式化字符串 我的字符串只包含像“1234.0”这样的数字,我想返回格式化的数字 例如,给定字符串“1234.0”,结果应为字符串“1234”。使用以下格式: DecimalFormat myFormatter = new DecimalFormat("####"); String output = myFormatter.format(value); 您可以找到更多您还可以使用正则表达式: String n = "1234.0"; n.replaceAll("\\.0*$",

如何在Java中格式化字符串

我的字符串只包含像“1234.0”这样的数字,我想返回格式化的数字


例如,给定字符串“1234.0”,结果应为字符串“1234”。

使用以下格式:

DecimalFormat myFormatter = new DecimalFormat("####");
String output = myFormatter.format(value);

您可以找到更多

您还可以使用正则表达式:

String n = "1234.0";
n.replaceAll("\\.0*$", "");

您只想删除小数点位置吗?不需要四舍五入吗?你尝试过什么?
String formatted=String.format(format,args…
确实会帮助你成为“懒惰的程序员”。程序员常用的一种方法是。。。编写代码。或者删除逗号后的每个字符(从字符串开头到逗号的子字符串)。
try {
   String formatted = String.valueOf((int)Double.parseDouble("12345.0"));
} catch (ParseException e) {
   // input is not a number
}