【问题标题】:How do I take the text from a TextView and format it for USD Currency?如何从 TextView 中获取文本并将其格式化为美元货币?
【发布时间】:2016-10-17 02:21:13
【问题描述】:

这就是我正在做的 -->

//get entered texts from the edittexts,and convert to integers.
Integer value1 = Integer.parseInt(sand1.getText().toString());


//doing a calculation
Double calculatedValue1 = (9.5*value1);


//set the value to the textview, to display on screen.
result1.setText("$"+ calculatedValue1.toString());

TextView 中的结果 1 现在看起来像这样:##.#

我需要它看起来像 $##.##

【问题讨论】:

    标签: java android textview currency number-formatting


    【解决方案1】:

    把你的代码改成这样:

    /get entered texts from the edittexts,and convert to integers.
    Integer value1 = Integer.parseInt(sand1.getText().toString());
    
    
    //doing a calculation
    Double calculatedValue1 = (9.5*value1);
    
    
    //set the value to the textview, to display on screen.
    result1.setText("$"+ String.format( "%.2f", calculatedValue1 ));
    

    【讨论】:

    • 我很高兴它有帮助。欢迎:)
    • 我遇到了一个更复杂的问题,我不知道该怎么问
    • 你可以来聊天
    • 基本上,我让用户再看到你在上面看到的代码十次。然后用户按下按钮并完成计算。从那里打印小计和总计。当用户第一次在编辑文本中输入数字时,按下按钮,然后向上滚动以显示他们的输入,然后再次按下计算按钮,应用程序“停止工作”
    • 你需要分享日志然后我才能说发生了什么
    【解决方案2】:

    您好,您可以使用这个(Locale.US 将数字格式化为美国标准,%.2f 用于十进制数):

    String.format(Locale.US, "%.2f", doubleValue);

    【讨论】:

    • 真棒@Casey:D!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 2011-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多