简体   繁体   中英

Android Java Decimal Format - Zero is hidden after decimal point

In my app, i want to be able to format numbers to string (with commas) in a TextView eg 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

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 plsclick here.

Try with:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM