I encountered a problem in android, for example:
Number 78900000 will show 7.8E7 and not the number itself.
DecimalFormat df2 = new DecimalFormat("#########.00");
dd2dec = new Double(df2.format(number).doubleValue());
How can I format the number and show it in original form without in scientific form ?
I believe this might help:
format("%,d", (number)); //Displays with commas placed appropriately.
format("%,d", (number)); //Displays un-formatted integer.
Android Formatting Documentation
Related
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));
}
I have the following data scanned from a pdf417 and need to extract certain text to certain text fields (already created), not sure how to go about this... Data scanned with manatee works plugin and android app using android studio.
All help will be appreciated.
Data that was returned from scan -
%MVL1CC18%0154%4025M003%4025012RP01C%DC62XBGP%NISSAN%SILVER/SILWER%
Each part between the %'s need to go to a text field. I know that I need to make use of String substr=mysourcestring.substring(startIndex,endIndex); but this will work up to the first 2 % signs. How do I continue to the next few?
Thanks.
If you want to split string based on a delimiter, use the following
String delimitter="%";
String[] parts = inputString.split(delimitter);
Why not use String.split()?
In your case it would look something like this:
String[] extractedStrings = mysourcestring.split("%");
You can work on your string by using split method:
String yourString = "%MVL1CC18%0154%4025M003%4025012RP01C%DC62XBGP%NISSAN%SILVER/SILWER%";
String[] split = yourString.split("%");
In this way you will get an array where each item is a substring between two % chars.
I am working on a project in which i am getting value from excel sheet(in assets android) and reflecting data in list view.
problem is:: phone no is not in proper format.
9.777123455E9 instead of 9777123455
When it's a phone number, you should always store the cell data as text, even if it consists only of digits, since a phone number is no mathematical number and when doing operations on it, you want to treat it as a string of characters (i.e. text).
If you input a phone number that looks to Excel like a mathematical number, it will interpret it as a number and in consequence will do things to it that make sense for numbers, but not necessarily for phone numbers, such as displaying it in scientific format.
To force Excel to treat your number as text, precede it with a single quote (apostrophe) when entering it. That is, enter into the cell:
'9777123455
It will be displayed without the single quote, just as you expect a phone number to be displayed and can be processed as text.
double d=9.777123455E9;
NumberFormat formatter = new DecimalFormat("#");
System.out.println(d);
System.out.println(formatter.format(d));
output
9.777123455E9
9777123455
E9 simply means multiply by 10^9
Update:
As #blubberdiblub mentioned, for phone numbers, it makes sense to change it to text. But for other cases, If you need to do mathematical operations leaving it in the scientific format works. You can right click on the column name and select formatting option to set the type of data the column will handle (number , text etc). If you want don't want to change the phone number to text and still see the number, simply increase the width of the column. The number will be shown full (without the "E").
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()));
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.