java - 如何从 TextView 中获取文本并将其格式化为美元货币?

标签 java android textview currency number-formatting

这就是我正在做的 -->

//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 现在看起来像这样:##.#

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

最佳答案

将您的代码更改为如下内容:

/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 ));

关于java - 如何从 TextView 中获取文本并将其格式化为美元货币?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37844405/

相关文章:

android - Android 谷歌地图中的线条和绘图绘制顺序

android - setOnItemClickListener() 方法在 fragment 中不起作用

android - TextView 失败

android - 我可以覆盖 Android 输入法处理自动完成和自动更正的方式吗?

java - 将 json 部分反序列化为 POJO,但保留一些原始 json 作为字段

java - Dagger 2 : Injected object might still be null before onAttach is called in fragment

java - 用于读取编辑器内容的 Eclipse 插件

java - 根据可用空间自动调整文本大小

java - 将 Spinner 选择保存到 SQLite 数据库中

java - 为什么只有 1 个对象适合 GC?