Floating Point is not correct - android

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.

Related

Getting improper data while perform devide operation in android

I am doing as below :
var value = 0.0
value = ((3300/1000).toDouble())
But getting value as 3.0 instead of 3.3
Why ?
Where am I doing mistake ? What I have to do to get the value as 3.3 ?
you are dividing integers and then converting them to a double afterwards, which doesn't work like you want it to.
3300/1000
these are integers, so no decimal values. 3300/1000 is just 3
3300.0/1000.0
these are doubles, so they have decimal values

EditText converting decimal to exponential

I am storing a value in database as 0.0004 and displaying the same in EditText but in edittext it as showing as 4.0E-4 tried a lot of things nothing seems to work for me.
Storing value in database is fine but when coming to edittext display it is wrong.
What I have tried:
Increased the size of edit text but didn't solve the problem.
Followed below process
DecimalFormat FORMATTER = new DecimalFormat("0.####");
textvalue.setText(REAL_FORMATTER.format(new_percent.getLong(new_percent.getColumnIndex(bt.column2))));
but the result is same no chage.
Can any one help to solve this..I want the display in edit text as 0.0004 not as 4.0E-4
Thanks for your time.
Instead of giving pattern to the BigDecimal just send directly the value to the constructor parameter.
example:
BigDecimal number = new BigDecimal("4.0E-4");
int ints= number.intValue(); //BigDecimal to integer
double doubles= number.doubleValue(); //BigDecimal to double
Why you retrieve 0.0004 value with the getLong mehod?
As I suppose, you store the 0.0004 in a database in a REAL column type,
so you should retrieve this with getDouble.
DecimalFormat formatter = new DecimalFormat("0.####");
textValue.setText(
formatter.format(
new_percent.getDouble(
new_percent.getColumnIndexOrThrow(bt.column2))));
Finally achieved as below.
BigDecimal.valueOf(Cursor.getDouble(Cursor.getColumnIndex(bt.column2)))

java Double.valueOf() returns number with one decimal place

I can't seem to get my head around this. If have tried the following approaches:
DecimalFormat df = new DecimalFormat("00000.00");
DecimalFormat df = new DecimalFormat("######.##");
But the following line always generates and IllegalArgumentException.
double price = Double.valueOf(df.format(((EditText) view
.findViewById(R.id.edit_item_price)).getText().toString()));
// Sample input passed is value of 200 or some other whole number.
item.setPrice(price);
It doesn't make sense as I only copied the obvious solutions in this forum. Most of you got the format() to work.
Originally, I didn't have these lines of code. I just call my setPrice() method after getting the item price. This works. However, Double.valueOf() has a nasty habit of using only one decimal position.
e.g. passed 200. I get 200.0 inside my item object. I figured by using DecimalFormat I could've prevented this but it appears this caused me MORE headaches instead.
When you say you pass 200 and you get 200.0, you mean you get it in a double value? If so, that doesn't matter - it's a number and 200 = 200.0 for double values.
format(...) turns a double value to a String value. You have it the other way round. That's why you get the Exception.
If the price variable is actually a double you should do
double price = Double.valueOf(((EditText) view
.findViewById(R.id.edit_item_price)).getText().toString())
But I think you want that the price is a String, then you should convert the text from the EditText to a double and that double back to a String with something like new DecimalFormat("0.00")

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

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

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.

Categories

Resources