[英]How to get this number format in java?
Lets say I am having a number as follows : 可以说我的电话号码如下:
long number = 32301672060;
I want the following the format for the number : 我想要以下格式的数字:
323.016.720,60
323.016.720,60
ie the last two digits should be separated by comma and then dot between every three digits. 也就是说,最后两位数字应以逗号分隔,然后在每三位数字之间加点。
Suppose the another number is : 假设另一个数字是:
long number = 139454
then output should be 然后输出应该是
1.394,54
1.394,54
Cast the value to a double
, divide it by 100 (to get the 2 decimal points) and then set the current locale to something like de_DE
and use NumberFormat to format it for you. 将值转换为
double
精度值,将其除以100(以获得2个小数点),然后将当前语言环境设置为de_DE
类的de_DE
并使用NumberFormat为您设置其格式。
Edit: As noted by Behrang in the comments, after converting the long to a double, it should only be used for display purposes as further calculations using this might result in loss of precision. 编辑:正如Behrang在评论中所指出的,在将long转换为double之后,应仅将其用于显示目的,因为使用该值进行进一步的计算可能会导致精度下降。
long number = 32301672060L;
NumberFormat nb = NumberFormat.getNumberInstance(Locale.GERMAN);
nb.setMinimumFractionDigits(2);
System.out.println(nb.format((double)number/100));
This should work for you. 这应该为您工作。 The German Local is important to have the point as decimal point and the comma at the last 2 digits.
对于小数点,逗号在最后两位数字是很重要的。
Use decimals. 使用小数。
final BigDecimal dec = new BigDecimal(BigInteger.valueOf(32301672060L), 2);
System.out.println(new DecimalFormat("###,##0.00").format(dec));
or instead of the pattern, better to use locale's formats, eg 或代替模式,最好使用语言环境的格式,例如
System.out.println(NumberFormat.getCurrencyInstance(Locale.US).format(dec));
or 要么
System.out.println(NumberFormat.getNumberInstance(Locale.US).format(dec));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.