Android Java 十进制格式 - 小数点后隐藏零

发布于 2025-01-14 04:37:46 字数 1182 浏览 3 评论 0原文

在我的应用程序中,我希望能够将 TextView 中的数字格式化为字符串(带逗号),例如 123456.000 到 123,456.000。但问题是,当我输入零(0)作为小数点后的最后一个数字时,它什么也不显示,直到我输入另一个数字。例如,如果我输入 123456.000,我会得到 123,456。直到我输入另一个数字(例如“1”)后,零才会显示,然后我得到 123,456.0001。请问如何让零显示为小数点后的最后一个数字?下面是一些示例代码。

我对代码的笨拙表示歉意。我是用手机手动输入的。

TextView txtView;
boolean NumberClicked = false;
String number = "";

// format pattern
DecimalFormat formatter = new DecimalFormat("#,###.#########");


// input dot (decimal point)
dot.setOnClickListener (new View.OnClickLstener() {
public void onClick(View view) {
if    (!txtView.getText.toString.contains(".") {
input(".");}}});

// input zero (0)
zero.setOnClickListener (new View.OnClickLstener() {
public void onClick(View view) {
input("0");
}});

// input one (1)
one.setOnClickListener (new 
View.OnClickLstener() {
public void onClick(View view) {
input("1");}});

// input method
public void input (String view) {
if (!numberClicked) {
number = view;
numberClicked = true;
} else {
number = number + view;
}
// print the entered numbers to
//the TextView after formatting 
if (!number.endsWith(".")) { txtView.setText(formatter.format(Double.parseDouble(number)));}}

在此输入代码

In my app, i want to be able to format numbers to string (with commas) in a TextView e.g 123456.000 to 123,456.000. But the problem is when i type zero (0) as the last number after the decimal point, it shows nothing until i type another number. For example, if i type 123456.000, i get 123,456. The zeros doesn't show until i type another number like "1" then i get 123,456.0001. Please how do i make zeros show as last number after decimal point? Below are some sample codes.

I apologise for the clumsiness of the code. I manually typed it with my phone.

TextView txtView;
boolean NumberClicked = false;
String number = "";

// format pattern
DecimalFormat formatter = new DecimalFormat("#,###.#########");


// input dot (decimal point)
dot.setOnClickListener (new View.OnClickLstener() {
public void onClick(View view) {
if    (!txtView.getText.toString.contains(".") {
input(".");}}});

// input zero (0)
zero.setOnClickListener (new View.OnClickLstener() {
public void onClick(View view) {
input("0");
}});

// input one (1)
one.setOnClickListener (new 
View.OnClickLstener() {
public void onClick(View view) {
input("1");}});

// input method
public void input (String view) {
if (!numberClicked) {
number = view;
numberClicked = true;
} else {
number = number + view;
}
// print the entered numbers to
//the TextView after formatting 
if (!number.endsWith(".")) { txtView.setText(formatter.format(Double.parseDouble(number)));}}

enter code here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

秋日私语 2025-01-21 04:37:46

您可以尝试这个代码片段:

DecimalFormat df = new DecimalFormat("#,##0.0#");
df.setMinimumFractionDigits(3);
String value = df.format(123456.000);

输出:123,456.000

有关更多信息,请点击此处。

You may try this code snippet:

DecimalFormat df = new DecimalFormat("#,##0.0#");
df.setMinimumFractionDigits(3);
String value = df.format(123456.000);

Output: 123,456.000

For more info pls click here.

川水往事 2025-01-21 04:37:46

尝试使用:

String.format("%,.03f", 123456.000)

Try with:

String.format("%,.03f", 123456.000)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文