Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.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 android-设置双精度格式并将其放在文本视图中,不带逗号(如果需要)_Java_Android_Format_Double_Comma - Fatal编程技术网

Java android-设置双精度格式并将其放在文本视图中,不带逗号(如果需要)

Java android-设置双精度格式并将其放在文本视图中,不带逗号(如果需要),java,android,format,double,comma,Java,Android,Format,Double,Comma,我在Double中从服务器获取“price”值,我需要将其放在我的应用程序的TextView中。问题是:当price=500时,我得到500.0,因为它是双倍的。我希望它看起来像500.55或500.50或只是500-如何以正确的方式格式化这些数字? 谢谢。使用String#format方法 请在此处阅读: 或者在JavaDoc中使用String#format方法 请在此处阅读: 或者在JavaDoc中,您需要使用方法intValue()显式地获取int值,如下所示: 双d=5.25; 整数i=

我在
Double
中从服务器获取“price”值,我需要将其放在我的应用程序的TextView中。问题是:当price=500时,我得到500.0,因为它是双倍的。我希望它看起来像500.55或500.50或只是500-如何以正确的方式格式化这些数字? 谢谢。

使用String#format方法

请在此处阅读: 或者在JavaDoc中使用String#format方法

请在此处阅读:
或者在JavaDoc中,您需要使用方法intValue()显式地获取int值,如下所示:

双d=5.25; 整数i=d.intValue()

或 双d=5.25;
int i=(int)d

您需要使用方法intValue()显式获取int值,如下所示:

双d=5.25; 整数i=d.intValue()

或 双d=5.25; int i=(int)d

使用

编辑

好的,然后尝试不同的方法:

public static String formatPrice ( double price){
    if (price == (long) price)
        return String.format("%d", (long) price);
    else
        return String.format("%s", price);
}
使用

编辑

好的,然后尝试不同的方法:

public static String formatPrice ( double price){
    if (price == (long) price)
        return String.format("%d", (long) price);
    else
        return String.format("%s", price);
}

您可以使用
rexgex
进行格式化

1.)创建一个函数以识别以下条件

  • 如果精度值仅包含零,则截断它们

  • 如果小数点后有任何非零值,则返回原始值

    public String formatValue(double d){
        String dStr = String.valueOf(d);
        String value = dStr.matches("\\d+\\.\\d*[1-9]\\d*") ? dStr : dStr.substring(0,dStr.indexOf("."));       
        return value;
    }
    
\\d+\.\\d*[1-9]\\d*
:匹配一个或多个数字,然后匹配一个

  • \\d*[1-9]\\d*
    :匹配一个非零值
测试用例

    yourTextView.setText(formatValue(500.00000000)); // 500
    yourTextView.setText(formatValue(500.0001));       // 500.0001
    yourTextView.setText(formatValue(500));          // 500
    yourTextView.setText(formatValue(500.1111));     // 500.1111

您可以使用
rexgex
进行格式化

1.)创建一个函数以识别以下条件

  • 如果精度值仅包含零,则截断它们

  • 如果小数点后有任何非零值,则返回原始值

    public String formatValue(double d){
        String dStr = String.valueOf(d);
        String value = dStr.matches("\\d+\\.\\d*[1-9]\\d*") ? dStr : dStr.substring(0,dStr.indexOf("."));       
        return value;
    }
    
\\d+\.\\d*[1-9]\\d*
:匹配一个或多个数字,然后匹配一个

  • \\d*[1-9]\\d*
    :匹配一个非零值
测试用例

    yourTextView.setText(formatValue(500.00000000)); // 500
    yourTextView.setText(formatValue(500.0001));       // 500.0001
    yourTextView.setText(formatValue(500));          // 500
    yourTextView.setText(formatValue(500.1111));     // 500.1111

根据
拆分字符串,然后删除
[1]
将拆分字符串定位到
然后删除
[1]
position@YagamiLight当然@当然可以。