Double variable format - android

I have a the next line :
String.format("%10.6f",transform);
I use String.format to force the value to be shown with 6 decimals and not with E.
But when transform equals to 0.040000 I want to have 0.04 as result. And for 0.000006 I want to have 0.000006.

Use NumberFormat instead of String.format():
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(6);
nf.format(yourNumber);

Related

How to Format double data from database and set into Textview using comma as decimal divider on Android

I have field on table as double (money data field), and i want to put it on Textview in format for example:
money= 20.0
i want to show as: 20,0
or money = 190.25
i want to show as: 190,25
how to do that? this is my variable
txtTotalBill.setText(String.valueOf(total_bill));
You can use the String method replace to format your chain :
txtTotalBill.setText(String.valueOf(total_bill).replace('.', ','));
String total = String.valueOf(total_bill);
total = total.replace(".", ",");
try this and then settext hope it helps
txtTotalBill.setText(total.toString());
Or you can directly setText without variable like this:
txtTotalBill.setText(String.valueOf(total_bill).replace('.', ','));

Truncate value after decimal point in android

I want to truncate the value from the given value 32.1500 into 32 and display it on textview text please help me out i searched a lot but did not found anything.
If you don't want to cast it you can keep it at double but truncate the trailing zeroes like this:
textView.setText(String.format("%.0f", 5.222));
Try to use cast like this.
double number=32.1500;
int numbereditted=(int) number;
textView.setText("Number= "+numbereditted);
Or use decimal format.
double number=32.1500;
DecimalFormat df=new DecimalFormat("0");
textView.setText("Number= "+df.format(number));
Try something like this:
private final DecimalFormat decimalFormat = new DecimalFormat("#.####");
private final DecimalFormat noDecimalFormat = new DecimalFormat("#");
String decimalValue = decimalFormat.format(value);
String noDecimalValue = noDecimalFormat.format(value);
Or using NumberFormat instead of DecimalFormat:
private NumberFormat formatter;
// Set the properties for the number format that will display the values
formatter = NumberFormat.getNumberInstance();
formatter.setMinimumFractionDigits(4);
formatter.setMaximumFractionDigits(4);
String decimalValue = formatter.format(value);
formatter.setMinimumFractionDigits(0);
formatter.setMaximumFractionDigits(0);
String noDecimalValue = formatter.format(value);
You can set the rounding mode you want like this:
noDecimalFormat.setRoundingMode(RoundingMode.DOWN)
formatter.setRoundingMode(RoundingMode.DOWN)

How to format a Double value properly with decimal separator?

I have an application that uses thousands separator (,) and decimal separator (.), I used this app on 2 tablets with the same languages (EspaƱol) on their configuration and when I do some process with numbers like 15,000.00 on the first one the answer was correct but in the second tablet the number changes to 15,00. I changed the language of the second to English, and it works, but how can i set this number format on code?
Sorry about the errors this is not my native language.
Thanks for the help
You could format a double value in code like this:
/**
* format a number properly
* #param number
* #return
*/
public String formatDecimal(double number) {
DecimalFormat nf = new DecimalFormat("###.###.###.##0,00");
// or this way: nf = new DecimalFormat("###,###,###,##0.00");
String formatted = nf.format(number);
return formatted;
}
And then set it to a TextView:
mytextview.setText("MyDouble: " + formatDecimal(somedouble));
You could format using
NumberFormat nf = NumberFormat.getInstance(new Locale("es", "MX")); //for example
But beware because depending on the locale, even if is the same language but different country the decimal char and grouping char my change, for example
NumberFormat nf = NumberFormat.getInstance(new Locale("es", "CO")); //displays 15.000,00
You can get an instance for a specific locale as specified by the android docs:
NumberFormat nf = NumberFormat.getInstance(Locale.FRENCH);
However, you should not change the locale when displaying stuff to the user imho.

only reading till 1st decimal digit

In my android app, I have an EditText in which the user enters a decimal which can be as long as the user wants. It can be a number like 25, 54.77, 23.7, 7.88, etc. In the same activity, I have a textView which reads the input into the EditText and displays the decimal but only till the first decimal digit. For e.g. if the user enters: 25 should be displayed as 25, 54.77 as 54.7, 23.7 as 23.7, 7.88 as 7.8. How can I achieve this? I tried using the following code but it didn't work:
NumberFormat df = DecimalFormat.getInstance();
df.setMinimumFractionDigits(0);
df.setMaximumFractionDigits(1);
df.format(EditTextNumber);
Use String.format:
String result = String.format("%.1f", yourdecimalvalue);
you can use DecimalFormat too:
NumberFormat formatter = new DecimalFormat("#0.0");
String result = formatter.format(yourdeciamlvalue);
Try NumberFormat instead of DecimalFormat:
NumberFormat nf = NumberFormat.getInstance();
nf.setMinimumFractionDigits(1);
nf.setMaximumFractionDigits(1);
TextView.setText(nf.format(editText.getText()));
if you are grbbing the input via an edittext then u can use this code
String number=input.getText().toString(); //input is ur input edittext
String output=number;
int indexOfDecimal=number.lastIndexOf('.');
if(indexOfDecimal!=-1&&indexOfDecimal<output.length()-1)
number=output.substring(0, indexOfDecimal+2); // use number as the output

Rounding up decimal in EditText

I'm trying to figure out how to round up the total to two decimal places. The total is an EditText. I can't get it to narrow down to just two decimal places. What wrong with it? Thanks for your help.
public void macro() {
caloriesTotal = Double.parseDouble(calories.getText().toString());
total = (caloriesTotal * .20)/9;
DecimalFormat round = new DecimalFormat ("###.##");
round.format(total);
carbsTotal = (caloriesTotal * .40)/4;
proteinTotal = (caloriesTotal * .40)/4;
fat.setText(Double.toString(total));
carbs.setText(Double.toString(carbsTotal));
protein.setText(Double.toString(proteinTotal));
}
Try this.
fat.setText(Double.valueOf(round.format(total)));
When you convert any number upto N number of decimal digit then you need to store that value in any variable or directly show like as above i mentioned.
Simply format() should do for you,
DecimalFormat round = new DecimalFormat ("###.##");
String formatted = round.format(double_value);
fat.setText(formatted);
You can use String.format("%.2f", d), your double will be rounded

Categories

Resources