i am new to the java, iam facing an issue in my task with DecimalFormat
when i give integer number it should not have any decimals,
if i give decimal number it should print decimal value
i used the DecimalFormater as below
DecimalFormat decimalFormat = (DecimalFormat) NumberFormat.getNumberInstance();
decimalFormat.setMaximumFractionDigits(2);
decimalFormat.applyPattern("0");
System.out.println(decimalFormat.format(123456789.2569));
here it is printing without decimals.
i replace my patern with
decimalFormat.applyPattern("#.##");
in this case when i give 299
it is printing 3
but it should print 299
how to do it?
finally, if i give 265 it should be 265, if i give 265.36 it should be 265.36
If you use the #.## pattern you will get only the first digit and the rest of your number will be treated as fraction digits.
And that explains why you got 3 when you inputed 299 because with this pattern 299 is treated as 2.99 and then rounded to 3.
So try to use this pattern instead:
decimalFormat.applyPattern("###.##");
For further information about it you can take a look at:
Customizing Formats in Documentation
Java DecimalFormat | tutorials.jenkov.com
Related
I have a double which contains values such as:
19.52
5.1
6.13
101.5
It will not contain more than 2 fractional digits. That is being checked for. If there are more than 2, the rest can be ignored.
I'd like to convert it to a String which has 4+ digits, like this:
"1952"
"0510"
"0613"
"10150"
How can I do that?
I'm using someone's API and the last 2 digits are specified as being always for the first 2 digits of the fractional part, that's why I need to do this.
You can multiply your number by 100 to remove the decimal point (because you only have 2 decimal digits), and then use the format 0000 to format it:
DecimalFormat df = new DecimalFormat("0000");
System.out.println(df.format(x * 100));
where x is your double.
I am currently using the BigDecimal and with variable number of digits after decimal point. I need to be able to format the number with loosing the number of digits which I had set, because when I use format then it reduces the decimal digits to 2 - 4.
str1 =bigDecimal.setScale(numberOfDecimalPlaces, RoundingMode.HALF_UP);
str2 = NumberFormat.getInstance().format(bigDecimal.setScale(numberOfDecimalPlaces, RoundingMode.HALF_UP));
The first list works fine but when I want to format the String/BigDecimal then it drops the decimal places.
Note: Decimal place will vary from 0 to 15(from user). I am using Android Studio-API15/Java. Precision is important in my app, formatting is to improve readability.
As mentioned by #JB Nizet, you need to tune NumberFormat acc. to your need.
Below is a working example:
int numberOfDecimalPlaces = 6;
BigDecimal bigDecimal = new BigDecimal(11212.122323);
bigDecimal.setScale(numberOfDecimalPlaces, RoundingMode.HALF_UP);
NumberFormat numberFormat = NumberFormat.getInstance();
numberFormat.setMinimumFractionDigits(numberOfDecimalPlaces);
System.out.println(numberFormat.format(bigDecimal));
Output:
11,212.122323
I'm turning this double into a string so I can display it on a TextView. I want the string to have 2 decimal places using String.format, but I don't know where to put it in this line of text.
Example.setText(Double.toString(dValue));
Any Ideas?
The quick and dirty way is to use a formatted String and specify the number of decimal points. Lately there's been a trend of suggesting the usage of a DecimalFormat instead since it will respect different locales and the usage of commas or points as a decimal separator.
//The suggested way lately
DecimalFormat formatter = new DecimalFormat("#0.00");
twoDecimals.setText(formatter.format(2.123145));
//The usual way with some caveats
twoDecimals.setText(String.format("%.2f",2.123));
I'm pretty sure it could also be done with formatted strings, but hey.. who am I to go against the trend.
Example.setText(String.format("%.2f", dValue));
.2 means 2 decimal places
f means it's a decimal type
Try this -
DecimalFormat df = new DecimalFormat("#.00");
Example.setText(df.format(dValue));
I'm using DecimalFormat in order to convert double to string like this:
DecimalFormat df = new DecimalFormat("###.###");
df.format(km);
The problem is, although the variable km has the value of 9.655195710258606E-5, the result of the df.format method returns 0 as string.But I expect something like 9.655 instead of 0.
Any help & suggestions will be appreciated
Thank you for your concern
It works as it should. The string "9.655" would be wrong. The value is actually 0.00009655 and if you round it to 3 decimal places you get "0".
To see some more digits, you can use more decimal places in the format #.###### or force scientific notation with a format like this #.###E0.
In the end of these numbers, the E(x) represents the exponent. E-5, means the exponent is -5, which means the actual number is 9.655 * (1/(10^5)).
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to round a number to n decimal places in Java
I am having difficulties rounding a float to two decimal places. I have tried a few methods I have seen on here including simply just using Math.round(), but no matter what I do I keep getting unusual numbers.
I have a list of floats that I am processing, the first in the list is displayed as 1.2975118E7. What is the E7?
When I use Math.round(f) (f is the float), I get the exact same number.
I know I am doing something wrong, I just am not sure what.
I just want the numbers to be in the format x.xx. The first number should be 1.30, etc.
1.2975118E7 is scientific notation.
1.2975118E7 = 1.2975118 * 10^7 = 12975118
Also, Math.round(f) returns an integer. You can't use it to get your desired format x.xx.
You could use String.format.
String s = String.format("%.2f", 1.2975118);
// 1.30
If you're looking for currency formatting (which you didn't specify, but it seems that is what you're looking for) try the NumberFormat class. It's very simple:
double d = 2.3d;
NumberFormat formatter = NumberFormat.getCurrencyInstance();
String output = formatter.format(d);
Which will output (depending on locale):
$2.30
Also, if currency isn't required (just the exact two decimal places) you can use this instead:
NumberFormat formatter = NumberFormat.getNumberInstance();
formatter.setMinimumFractionDigits(2);
formatter.setMaximumFractionDigits(2);
String output = formatter.format(d);
Which will output 2.30
You can make use of DecimalFormat to give you the style you wish.
DecimalFormat df = new DecimalFormat("0.00E0");
double number = 1.2975118E7;
System.out.println(df.format(number)); // prints 1.30E7
Since it's in scientific notation, you won't be able to get the number any smaller than 107 without losing that many orders of magnitude of accuracy.
Try looking at the BigDecimal Class. It is the go to class for currency and support accurate rounding.