How to print a double with two decimals in Android? [duplicate] - android

This question already has answers here:
How to round a number to n decimal places in Java
(39 answers)
Closed 6 years ago.
Maybe this is a silly question, but I cannot guess how to solve it if it's not creating a method. Maybe there's a "natural way" to do it, like in C for example. Here's the problem:
I have a var:
double a;
And I want to show it only with 2 or 3 decimals. When I try to show it:
Text.setText("Value of a: " + String.valueOf(a));
It gives something like:
Value of a: 5.234966145
And i would want just
Value of a: 5.23
Without changing the real value of a so it shows the approximate number but works with the real number.

yourTextView.setText(String.format("Value of a: %.2f", a));

For Displaying digit upto two decimal places there are two possibilities -
1) Firstly, you only want to display decimal digits if it's there.
For example - i) 12.10 to be displayed as 12.1, ii) 12.00 to be displayed as 12. Then use-
DecimalFormat formater = new DecimalFormat("#.##");
2) Secondly, you want to display decimal digits irrespective of decimal present For example -i) 12.10 to be displayed as 12.10. ii) 12 to be displayed as 12.00.Then use-
DecimalFormat formater = new DecimalFormat("0.00");

You can use a DecimalFormat, or String.format("%.2f", a);

Before you use DecimalFormat you need to use the following import or your code will not work:
import java.text.DecimalFormat;
The code for formatting is:
DecimalFormat precision = new DecimalFormat("0.00");
// dblVariable is a number variable and not a String in this case
txtTextField.setText(precision.format(dblVariable));

textView2.setText(String.format("%.2f", result));
and
DecimalFormat form = new DecimalFormat("0.00");
textView2.setText(form.format(result) );
...cause "NumberFormatException" error in locale for Europe because it sets result as comma instead of point decimal - error occurs when textView is added to number in editText.
Both solutions are working excellent in locale US and UK.

Related

How to format currency number to 2 decimal places, or none if no cents?

I want to format a currency number to 2 decimal places if there are cents, or no decimal places if there are none.
For example, 1 would show as $1.
1.1 would show as $1.10.
Is there an easy way to do this in Android with Kotlin?
I've used DecimalFormat("$#,###,##0.##). The main issue with this is 1.1 would appear as $1.1. Tried to also use DecimalFormat($#,###,##0.#0) app crashes because it says I can't after the 0 after the # at the 12th position.
Since Java and Kotlin work hand in hand here is a Java Solution
One way of doing this would be to check if decimal number is similar to its int or long value
if (number == (int) number)
{
NumberFormat formatter = new DecimalFormat ("##");
System.out.println (formatter.format (number));
}
else
{
NumberFormat formatter = new DecimalFormat ("##.00");
System.out.println (formatter.format (number));
}

How to displaying Float/Double in meaningful form in Android

I am trying to make a BMI application. When I run the application the BMI values are displayed in numeral form that I don't understand. I have tried both Float and Double type but results are same.
For example:
Height (m): 2
Weight (Kg): 100
BMI is displayed as : 2.0E-4 instead of 25
The part of the code that effects this is:
String editText1= height_field.getText().toString();
String editText2= weight_field.getText().toString();
try { // Parse string to int
double height = Double.parseDouble(editText1);
double weight = Double.parseDouble(editText2);
double bmi_result = (weight/(height*height));
String bmi_text = Double.toString(bmi_result);
display.setText(bmi_text);
System.out.println("OnClick: computeButton is clicked");
}
catch(NumberFormatException nfe) {
alert.show(); // Show error alert
To answer your original question, you should be using java.text.DecimalFormat, something like:
DecimalFormat formatter = new DecimalFormat("##.##");
display.setText(formatter.format(bmi_result));
Will force the result to be in the format of two digits followed by two decimal points, the table in the link above shows how to generate that.
However, since 2.0E-4 is 0.0002, I think Jon Skeet's comment is correct: You're doing your math operation wrong, since the value you're printing is a very small fraction of 25 :)
I'd recommend using Log.v() to print out your math operation before you actually do it, so you can see what the values of weight and height actually are, I highly doubt they're correctly set at what you described in the question.

Floating Point is not correct

I am getting a value from server that is not containing any Floating point let say its
1234 and have to cvonvert it in Floating value with 2 decimal point like 12.34.
Right now what i am doing is getting value storing it in float that convert the current value 1234 to 1234.0
after that doing this
tempB=Math.floor(tempB)/100.0;
DecimalFormat df = new DecimalFormat("###.##");
RewardsBalance=df.format(tempB);
But with this i m having an issue that when i have value such that 1230 it results in 12.3 not that 12.30
but when i have value 1234 it gives the desired result that is 12.34
so what step i m missing any clue
12.3 and 12.30 are the same value. The problem is not the value but the code that incorrectly converts the right value to the wrong representation. You probably want "###.00". With "#", zero shows as absent.
Use this it will work
Two digits after point
Try this :
String.format("%.2f", your_value);
It will do just like you want
try this
tempB=Math.floor(tempB)/100.0;
DecimalFormat df = new DecimalFormat("0.00");
df.format(tempB);
it will work fine.

android big decimal approximation

I want to approximate a number showing only two digits after the decimal point.
I wrote this:
String result = String.valueOf(new BigDecimal(price).setScale(4, BigDecimal.ROUND_UP).doubleValue());
If price is a decimal number like 63,9222, it works fine. But If price is ,for example, equal to 7.00, is printed 7.0 .
I tried to use RoundingMode.CEILING instead of BigDecimal.ROUND_UP , but it didn't work and i don't know why. Maybe it will be better to use Double instead of BigDecimal?
Use String.format
double myValue = new BigDecimal(price).setScale(4, BigDecimal.ROUND_UP).doubleValue());
String result = String.format(Locale.ENGLISH, "%.2f", myValue);

Show very big double values on EditText

First of all please excuse me for my bad English speaking.
I am new to Android Development. I have a problem and think you can solve it.
The problem is:
I have a very big double value like 12345678987654321 in my android app
but when i want to show it on EditText, it will be shown like this 12345678987654300.
In this case when my value characters is over than 15 chars android shows remaining chars with "0"
i don't know what i have to to do.
i am using this code:
DecimalFormat df = new DecimalFormat("#.#########");
double a = Distancevals[1] * Distancevals[2];
//Distancevals is an array of double with big values
EditText editto = (EditText)findViewById(...);
editto.setText(df.format(a));
Double stores your number 12345678987654321 in format 1.23456789876543E16, so you lose the end of the number 21. When you format the result it's known that your number consists of 17 signs, so format function adds two zeros in the end of your number instead of 21.
Try to use this:
DecimalFormat df = new DecimalFormat("#.#########");
BigDecimal a = new BigDecimal(Distancevals[1] * Distancevals[2]);
// Distancevals is an array of double with big values
EditText editto = (EditText) findViewById(...);
editto.setText(df.format(a));
Try this one
DecimalFormat df= new DecimalFormat("###############00");
double a=Distancevals[1]*Distancevals[2];
//Distancevals is an array of double with big values
EditText editto=(EditText)findViewById(...);
editto.setText(df.format(a));
You just show it as editto.setText(a+"");
Cause in your case EditText is not doing anything but DecimalFormat is changing your number.
I suggest that in the line:
editto.setText(df.format(a));
Change it to:
editto.setText(df.format(a.toString()));

Categories

Resources