Android 更改类似*******的双精度格式**

Android 更改类似*******的双精度格式**,android,double,Android,Double,我有双倍的钱。将金额设为500000.12。我想将金额的值设置为TextView,格式如下500000.12(其中12是美分,500000是美元) 我编写了这个函数,它可以工作 private String getAmountAsString(double amount) { double integralPart = amount % 1; int fractionalPart = (int) (amount - integralPart); i

我有双倍的钱。将金额设为500000.12。我想将金额的值设置为
TextView
,格式如下500000.12(其中12是美分,500000是美元)

我编写了这个函数,它可以工作

private String getAmountAsString(double amount) {
        double integralPart = amount % 1;
        int fractionalPart = (int) (amount - integralPart);
        int integral = (int) integralPart * 100;
        String strFractional = String.format("%,d", fractionalPart);
        String strAmount = (strFractional + "." + String.valueOf(integral));

        return strAmount;
    }

但是我认为,可以用一些简单而好的方法来实现这一点。有人能帮忙找到函数或更好的方法吗?

因此使用了
NumberFormat
s。他们善于处理不同国家的当地差异

//define a local, gets automated if a point or comma is correct in this Country.
NumberFormat anotherFormat = NumberFormat.getNumberInstance(Locale.US);
DecimalFormat anotherDFormat = (DecimalFormat) anotherFormat;
anotherDFormat.applyPattern("#.00");//set the number of digits afer the point
anotherDFormat.setGroupingUsed(true);// set grouping
anotherDFormat.setGroupingSize(3);//and size of grouping
double myDouble = 123456.78;
String numberWithSeparators = anotherDFormat.format(myDouble);//convert it

因此使用了
NumberFormat
s。他们善于处理不同国家的当地差异

//define a local, gets automated if a point or comma is correct in this Country.
NumberFormat anotherFormat = NumberFormat.getNumberInstance(Locale.US);
DecimalFormat anotherDFormat = (DecimalFormat) anotherFormat;
anotherDFormat.applyPattern("#.00");//set the number of digits afer the point
anotherDFormat.setGroupingUsed(true);// set grouping
anotherDFormat.setGroupingSize(3);//and size of grouping
double myDouble = 123456.78;
String numberWithSeparators = anotherDFormat.format(myDouble);//convert it

可以使用各种语言环境设置浮动、双精度浮动等格式。您可以使用:

String.format(Locale.<Your Locale>, "%1$,.2f", myDouble);

有关更多信息,请查看:,和

各种语言环境可用于格式化
浮点、双精度等。您可以使用:

String.format(Locale.<Your Locale>, "%1$,.2f", myDouble);

想了解更多信息,请看:,我想你必须看一下这个


我想你得看看这个


非常感谢,这就是我想要的。一行,而不是写一些函数。+1表示问题的答案。我不知道地点。我应该为区域设置提供什么值?非常感谢,这就是我想要的。一行,而不是写一些函数。+1表示问题的答案。我不知道地点。我应该为区域设置提供什么值?