I'd like to convert a BigDecimal to String for printing purposes but print out all digits without scientific notation. For example:
BigDecimal d = BigDecimal.valueOf(12334535345456700.12345634534534578901);
String out = d.toString(); // Or perform any formatting that needs to be done
System.out.println(out);
I'd like to get 12334535345456700.12345634534534578901 printed. Right now I get: 1.23345353454567E+16.
To preserve the precision for a BigDecimal you need to pass the value in as a String
BigDecimal d = new BigDecimal("12334535345456700.12345634534534578901");
System.out.println(d.toPlainString());
The BigDecimal class has a toPlainString method. Call this method instead of the standard toString and it will print out the full number without scientific notation.
Example
BigDecimal b = new BigDecimal("4930592405923095023950238502395.3259023950235902");
System.out.println(b.toPlainString());
Output: 4930592405923095023950238502395.3259023950235902
You want to use a DecimalFormat:
DecimalFormat df = new DecimalFormat("#.#");
String output = df .format(myBD);
System.out.println(value + " " + output);
You could use this
BigDecimal d = BigDecimal.valueOf(12334535345456700.12345634534534578901);
String out= d.toPlainString();
System.out.println(out);
Related
I am trying to make a number in a BigDecimal variable match the format of ###.# where no matter what number is passed it will come out as ###.#
For example, if I was passed the number 1 in a BigDecimal variable the method would return 001.0
If I was passed 11.1 as a BigDecimal variable the method would return 011.1
I already have a bit of the code to make the decimal places match
BigDecimal x = new BigDecimal(1);
DecimalFormat df = new DecimalFormat("#.0");
String formatted = df.format(x);
So this would return 1.0 however I cannot figure out how to make the leading zeros appear before converting back to BigDecimal.
Can someone help me out or point me in the right direction?
Thanks everyone
Use a NumberFormat instance, with a Locale which uses the dot as decimal separator.
final BigDecimal x = new BigDecimal("11.1");
final NumberFormat nf = NumberFormat.getNumberInstance(Locale.ENGLISH);
nf.setMinimumFractionDigits(1);
nf.setMaximumFractionDigits(1);
nf.setMinimumIntegerDigits(3);
final String formatted = nf.format(x);
NumberFormat is basically a Factory, so for example in this case the underlying formatter returned by getNumberInstance is a DecimalFormat. I always prefer being abstracted off the real implementation.
You can also decide if you want to display the grouping comma or not.
nf.setGroupingUsed(true); // 123,456.0 - default value is true
nf.setGroupingUsed(false); // 123456.0
Use new DecimalFormat("000.0")
Examples
DecimalFormat df = new DecimalFormat("000.0", DecimalFormatSymbols.getInstance(Locale.US));
System.out.println(df.format(new BigDecimal("1"))); // prints: 001.0
System.out.println(df.format(new BigDecimal("11.1"))); // prints: 011.1
System.out.println(df.format(new BigDecimal("123.456"))); // prints: 123.5
System.out.println(df.format(new BigDecimal(".07"))); // prints: 000.1
System.out.println(df.format(new BigDecimal("123456"))); // prints: 123456.0
You can use the StringUtils of apache.
StringUtils.leftPad(number.toString(), 3, '0');
Where the first argument is the String to format, second argument is the number of to left places to validate, and the last argument is the char for complete.
Reggards.
DecimalFormat df2 = new DecimalFormat("#.##");
double zipf = 0.23951367781155017;
String zipt = df2.format(zipf);
System.out.println(zipt);
And I get "0,24"
The problem with this is then I want to use it as a double. But the Double.valueOf(); method fails due to the comma being there in the string output. Any way to solve this?
For decimal dot, you should create an instance with english locale like this:
NumberFormat nf = NumberFormat.getNumberInstance(Locale.ENGLISH);
nf.setMinimumFractionDigits(2);
nf.setMaximumFractionDigits(2);
String zipt = nf.format(zipf);
System.out.println(zipt);
I also suggest setting rounding to HALF_UP, because default rounding is not what most of us would expect: http://docs.oracle.com/javase/1.5.0/docs/api/java/math/BigDecimal.html#ROUND_HALF_EVEN
nf.setRoundingMode(RoundingMode.HALF_UP);
Use different locale.German has dot
NumberFormat nf = NumberFormat.getNumberInstance(Locale.GERMAN);
DecimalFormat df = (DecimalFormat)nf;
Alternative woud be to use string and then modify string to your needs.After that just parse to double.All done :)
Your problems is the local that your JVM is using , try to change at your current local.
Use DecimalFormat constructor that allows you to specify locale
new DecimalFormat("#.##", new DecimalFormatSymbols(new Locale("en")));
you could "format" your double manually but cutting of the decimal places like this:
DecimalFormat df2 = new DecimalFormat("#.##");
double zipf = 0.23951367781155017;
String zipt = df2.format(zipf);
System.out.println(zipt);
long zipfLong = Math.round(zipf*100);
double zipfDouble = zipfLong/100.0;
System.out.println(zipfDouble);
with Math.round you make sure the that 0.239.. becomes 0.24. zipf*100 will "cut" off the additional decimal places and zipfLong/100.0 will add the decimal places again. Sorry, bad explanation but here is the output:
0,24
0.24
And you can reuse the new zipfDouble as a double value without casting or taking care of locale settings.
I am trying to get 123456789.12 value out of 123456789.1234
If i use strings, it will be worked straightaway using parsing logic. I don't want to return String. requirement is to return Long.
Here i would like return the output the Long type rather than string..can someone help me on this?
Long is an integer type, and thus can not hold a decimal.
Trying to use Long.parseLong would result in a NumberFormatException.
You need to use a representation that deals with the scale as well.
try this for long output:
String str = "123456789.1234";
BigInteger num = new BigInteger(str);
return num.longValue();
try this for float output:
String str = "123456789.1234";
BigDecimal num = new BigDecimal(str).setScale(2, BigDecimal.ROUND_HALF_UP);
return num.floatValue();
The number which you want to return it is not Long.You can return double or float.Also you can use
DecimalFormat df = new DecimalFormat("#.##");
String stringDouble = df.format(yourDouble);;
Can someone please tell me why this doesn't work? I'm confident it's because i'm trying to format a string to two decimal places.. but i don't know how else to make the output rounded to decimal places.
DecimalFormat df = new DecimalFormat("#.##");
String sTotalCost = Double.toString(totalCost);
txtTotalCost.setText("£" + df.format(sTotalCost));
Don't convert it to a string before formatting:
DecimalFormat df = new DecimalFormat("#.##");
txtTotalCost.setText("£" + df.format(totalCost));
If you just like to round for displaying purpose (as String), you did it correct. Just set the Roundmode that fits for you:
DecimalFormat df = new DecimalFormat("#.##");
df.setRoundingMode(RoundingMode.HALF_EVEN);
txtTotalCost.setText("£" + df.format(totalCost));
See: DecimalFormat rounding
If you like to calculate on the rounded variable, you should round it like this:
double roundedTotalCost = Math.round(totalCost*100.0)/100.0
See: http://www.mkyong.com/java/how-to-round-double-float-value-to-2-decimal-points-in-java/
I have a BigDecimal number and i consider only 2 decimal places of it so i truncate it using:
bd = bd.setScale(2, BigDecimal.ROUND_DOWN)
Now I want to print it as String but removing the decimal part if it is 0, for example:
1.00 -> 1
1.50 -> 1.5
1.99 -> 1.99
I tried using a Formatter, formatter.format but i always get the 2 decimal digits.
How can I do this? Maybe working on the string from bd.toPlainString()?
I used DecimalFormat for formatting the BigDecimal instead of formatting the String, seems no problems with it.
The code is something like this:
bd = bd.setScale(2, BigDecimal.ROUND_DOWN);
DecimalFormat df = new DecimalFormat();
df.setMaximumFractionDigits(2);
df.setMinimumFractionDigits(0);
df.setGroupingUsed(false);
String result = df.format(bd);
new DecimalFormat("#0.##").format(bd)
The below code may help you.
protected String getLocalizedBigDecimalValue(BigDecimal input, Locale locale) {
final NumberFormat numberFormat = NumberFormat.getNumberInstance(locale);
numberFormat.setGroupingUsed(true);
numberFormat.setMaximumFractionDigits(2);
numberFormat.setMinimumFractionDigits(2);
return numberFormat.format(input);
}
Use stripTrailingZeros().
This article should help you.
If its money use:
NumberFormat.getNumberInstance(java.util.Locale.US).format(bd)