Show very big double values on EditText - android

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

Related

How can I clip the first two digits of a number box item?

I have a program and a layout for coordinate input.
I'm entering latitude and longitude values. When the cordinates are close to each other (for example lat1:21.424521 and lat2:21.424534 ), and it gets really annoying to input all coordinates.
Does anyone know how can I get first two digits of the first values so i dont have write them multiple times.
double lat1 = 21.424521;
lat1 = new DecimalFormat("##.##").format(lat1));
This will, for example, trimm it down to two decimals. If you want only 2 digits, you would do it like this:
lat1 = new DecimalFormat("##").format(lat1));
And so on...
new DecimalFormat("#.##").format(dblVar);
That is what you need, if I understand your question correctly.
Example:
DecimalFormat df = new DecimalFormat("#.##");
df.setRoundingMode(RoundingMode.DOWN);
s = df.format(lat); // or df.format(lng);
Source:
How can I truncate a double to only two decimal places in Java?

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")

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.

Decimal Places using double with toString, android

I am creating a simple program that performs simple math functions on the values a user enters in the EditText views. The first two EditText views contain integers and the last could be a decimal, thus the answer could also need to be in decimal form so I set the and (vis) as a double, but how can I limit the decimal places to four? Everything is running fine, the answer is just many decimal places long!
Here is my code:
public void onClick(View v) {
String a,b,t;
double vis;
EditText txtbox1 = (EditText)findViewById(R.id.A);
EditText txtbox2 = (EditText)findViewById(R.id.B);
EditText txtbox3 = (EditText)findViewById(R.id.t);
TextView tv = (TextView) findViewById(R.id.Answer);
a = txtbox1.getText().toString();
b = txtbox2.getText().toString();
t = txtbox3.getText().toString();
vis = ((Integer.parseInt(a)*1) + (Integer.parseInt(b)*2)) / (Double.parseDouble(t));
tv.setText(double.toString(vis));
}
}
Thanks so much!
You could use String.format() to make sure you only get 4 decimal places in your output. Simply replace the last line with tv.setText(String.format("%.4f", vis));.
See http://download.oracle.com/javase/tutorial/java/data/numberformat.html for more details on how to use String.format() for this purpose.
I think it's a bit too late to answer, but it may help for future purpose.
If we have a double number and we need to get the 4 decimal values we can multiply the double by 10000, and cast the double value into an integer and reverse into double again and divide the digit by 10000.
For more control, use BigDecimal.round(). You can set a MathContext with the precision and rounding rule you require (.5 is round up, vs .5 is rounded down, etc).
double vis = 21.23456;
tv.setText(new BigDecimal(vis).round(new MathContext(6, RoundingMode.HALF_UP)).toString());

Categories

Resources