如何使用Formatter在java中格式化字符串
我正在尝试格式化字符串。我添加了用于格式化的代码。我有个例外 我希望所有这些的结果都是01,00。 例外情况-如何使用Formatter在java中格式化字符串,java,android,decimalformat,Java,Android,Decimalformat,我正在尝试格式化字符串。我添加了用于格式化的代码。我有个例外 我希望所有这些的结果都是01,00。 例外情况- public class Test { public static void main(String[] args) { final String test1 = new String("01,"); final String test2 = new String("01,0"); final String test3 = new
public class Test {
public static void main(String[] args) {
final String test1 = new String("01,");
final String test2 = new String("01,0");
final String test3 = new String("1,00");
String pattern = "##,##";
DecimalFormat formatter;
DecimalFormatSymbols dfs = new DecimalFormatSymbols();
dfs.setGroupingSeparator(',');
formatter = new DecimalFormat(pattern, dfs);
String result1 = formatter.format(test1);
String result2 = formatter.format(test2);
String result3 = formatter.format(test3);
System.out.println("Result 1 == " + result1);
System.out.println("Result 2 == " + result2);
System.out.println("Result 3 == " + result3);
}
}
如果有人有任何想法,请指导我。
十进制格式
只接受日期
或数字
对象,而不接受字符串
编辑-1:
1) String pattern=“00.00”
(二)
例如:
为了
它给了我:
final String test1 = new String("01,");
final String test2 = new String("02,3");
final String test3 = new String("1,00");
这就是应该如何使用它
Result 1 == 01,00
Result 2 == 02,30
Result 3 == 01,00
请不要在Java中使用
newstring(String)
,它不仅毫无意义,而且不必要地冗长。格式化程序的类型是什么。。java.util.Formatter?我使用了这个方法,但它给出的编译错误。format方法有2个和3个参数…它没有单个参数argument@Scorpion,什么编译错误?这只是DecimalFormat formatter=newdecimalformat(pattern,dfs)
很好,有人能解释一下他的“-1”吗?
final String test1 = new String("01,");
final String test2 = new String("02,3");
final String test3 = new String("1,00");
Result 1 == 01,00
Result 2 == 02,30
Result 3 == 01,00
format = new DecimalFormat(".00");
format.format(10.0);
String s = (String.format("%,d", 1000000)).replace(',', ' ');
int minutes = (int) secondsLeft / 60;
int seconds = secondsLeft - minutes * 60;
String upDatePattern ;
upDatePattern = (String.format("%02d:%02d", minutes,seconds));
timerTextView.setText(upDatePattern);
this produces a zero padded "timer" with min/sec
ex: 02:35
you can find lots of formatting examples on "Formatter | Android Developers