double d = 199.999; String s = String.valueOf(d); int index = s.indexOf("."); String s1 = s.substring(0, index + 2).replace(".", ","); System.out.println(s1);
for example double d = 199.999; String s = String.valueOf(d).replaceAll("(\\d+)[.](\\d)\\d+", "$1,$2"); System.out.println(s);
double d = 199.999; String s = String.valueOf(d); int index = s.indexOf("."); String s1 = s.substring(0, index + 2).replace(".", ","); System.out.println(s1);
很明显,使用德国风俗就行了。 import java.util.*; public class Test { public static void main(String args[]) { Locale de = new Locale("de","DE");//使用德国风俗。 Locale old = Locale.getDefault(); Locale.setDefault(de); System.out.printf("Germany locale %.1f", 19.999-0.05);//java是四舍五入的,我没找到开关所以用个小技巧就能保证是舍去。 Locale.setDefault(old); System.out.println(); System.out.printf("China locale %.1f", 19.999-0.05);//同上
double d = 199.999;
String s = String.valueOf(d);
int index = s.indexOf(".");
String s1 = s.substring(0, index + 2).replace(".", ",");
System.out.println(s1);
double d = 199.999;
String s = String.valueOf(d).replaceAll("(\\d+)[.](\\d)\\d+", "$1,$2");
System.out.println(s);
String s = String.valueOf(d);
int index = s.indexOf(".");
String s1 = s.substring(0, index + 2).replace(".", ",");
System.out.println(s1);
2. 四舍五入转int
3. 转字符串
4. 倒数第二位那里插入一个,分隔符
import java.util.*;
public class Test {
public static void main(String args[]) {
Locale de = new Locale("de","DE");//使用德国风俗。
Locale old = Locale.getDefault();
Locale.setDefault(de);
System.out.printf("Germany locale %.1f", 19.999-0.05);//java是四舍五入的,我没找到开关所以用个小技巧就能保证是舍去。
Locale.setDefault(old);
System.out.println();
System.out.printf("China locale %.1f", 19.999-0.05);//同上
}
}
具体参看《java核心技术第2卷》国际化那章。
BigDecimal n2 = n.setScale(1, BigDecimal.ROUND_FLOOR);
String str = n2 + "";
str = str.replace(".",",");
System.out.println(str);
float scale = 34.236323;
DecimalFormat fnum = new DecimalFormat("##0.0");
String dd=fnum.format(scale);
System.out.println(dd);
然后部分:replace(".",",")