Questions tagged [decimalformat]

The Java class DecimalFormat is a concrete subclass of NumberFormat that is used to format decimal numbers. Use this tag only for questions about this Java class, not for general decimal formatting.

DecimalFormat is a concrete subclass of NumberFormat that formats decimal numbers.

It has a variety of features designed to make it possible to parse and format numbers in any locale, including support for Western, Arabic, and Indic digits. It also supports different kinds of numbers, including integers (123), fixed-point numbers (123.4), scientific notation (1.23E4), percentages (12%), and currency amounts ($123). All of these can be localized.

654 questions
229
votes
8 answers

How to change the decimal separator of DecimalFormat from comma to dot/point?

I have this little crazy method that converts BigDecimal values into nice and readable Strings. private String formatBigDecimal(BigDecimal bd){ DecimalFormat df = new DecimalFormat(); df.setMinimumFractionDigits(3); …
MiraFayless
  • 2,353
  • 3
  • 15
  • 8
125
votes
18 answers

How can I truncate a double to only two decimal places in Java?

For example I have the variable 3.545555555, which I would want to truncate to just 3.54.
Johnny
  • 1,419
  • 3
  • 13
  • 13
63
votes
4 answers

How can I parse a String to BigDecimal?

I have this String: 10,692,467,440,017.120 (it's an amount). I want to parse it to a BigDecimal. The problem is that I have tried both DecimalFormat and NumbeFormat in vain.
BenMansourNizar
  • 1,558
  • 4
  • 21
  • 42
60
votes
7 answers

Change DecimalFormat locale

I have custom DecimalFormat in Edittext's addTextChangedListener method, everything is working perfectly but when I change language (locale) my addTextChangedListener is not working. double answer = inputDouble * counterToDouble; DecimalFormat df =…
BekaKK
  • 2,173
  • 6
  • 42
  • 80
51
votes
6 answers

Laravel validate decimal 0-99.99

Laravel does not have a validation for decimal, so I need a regex or other validation method to validate a numeric value of 0 - 99.99 I have tried…
Kender
  • 1,191
  • 3
  • 15
  • 34
45
votes
6 answers

Format number using decimal format in kotlin

I am facing an issue where I need to do some calculations with a number like for example 5000,00 multiplied it by (1,025^3). So in this case 5000,00 * (1,025^3) = 5385,45 So my question is, how can I format the number 5385,45 to be like 5.385,45…
José Nobre
  • 739
  • 2
  • 7
  • 16
38
votes
8 answers

Show padding zeros using DecimalFormat

I'm using DecimalFormat to format doubles to 2 decimal places like this: DecimalFormat dec = new DecimalFormat("#.##"); double rawPercent = ( (double)(count.getCount().intValue()) / (double)(total.intValue()) ) *…
mportiz08
  • 10,206
  • 12
  • 40
  • 42
31
votes
10 answers

How to use Java's DecimalFormat for "smart" currency formatting?

I'd like to use Java's DecimalFormat to format doubles like so: #1 - 100 -> $100 #2 - 100.5 -> $100.50 #3 - 100.41 -> $100.41 The best I can come up with so far is: new DecimalFormat("'$'0.##"); But this doesn't work for case #2, and instead…
Peter
  • 837
  • 3
  • 14
  • 19
20
votes
3 answers

Stop DecimalFormat with percentage sign from moving the decimal

Is there a way to prevent a DecimalFormat object from automatically moving the decimal place two places to the right? This code: double d = 65.87; DecimalFormat df1 = new DecimalFormat(" #,##0.00"); DecimalFormat df2 = new DecimalFormat(" #,##0.00…
ryvantage
  • 13,064
  • 15
  • 63
  • 112
19
votes
10 answers

DecimalFormat and Double.valueOf()

I'm trying to get rid of unnecessary symbols after decimal seperator of my double value. I'm doing it this way: DecimalFormat format = new DecimalFormat("#.#####"); value = Double.valueOf(format.format(41251.50000000012343)); But when I run this…
George
  • 8,368
  • 12
  • 65
  • 106
18
votes
2 answers

Rounding with DecimalFormat in Java

Let's look at the following statements in Java. System.out.println(new DecimalFormat("0").format(2.4)); //returns 2 System.out.println(new DecimalFormat("0").format(2.5)); //returns 2 <---Concentrate here System.out.println(Math.round(2.5)); …
Lion
  • 18,729
  • 22
  • 80
  • 110
17
votes
6 answers

Formatting numbers using DecimalFormat

I am trying to format prices using DecimalFormat, but this isn't working for all variations. DecimalFormat df = new DecimalFormat("0.##") df.format(7.8) df.format(85.0) prints 7.80 and 85 but "7.79999" gets formatted as "7.8", not "7.80". I have…
Jon
  • 3,174
  • 11
  • 39
  • 57
17
votes
2 answers

Java: Use DecimalFormat to format doubles and integers but keep integers without a decimal separator

I'm trying to format some numbers in a Java program. The numbers will be both doubles and integers. When handling doubles, I want to keep only two decimal points but when handling integers I want the program to keep them unaffected. In other…
Lefteris008
  • 899
  • 3
  • 10
  • 29
16
votes
4 answers

Number of significant digits in scientific notation with DecimalFormat

I'm trying to understand fully the exact behaviour of DecimalFormat. I'm currently making some tests with the scientific notation capabilities of this class. And I'm facing a problem with the tuning of the exact number of significant digits in…
Agemen
  • 1,525
  • 9
  • 18
15
votes
4 answers

Why was the pattern string not followed in this code?

When following code is executed: DecimalFormat eNotation1 = new DecimalFormat("#0.###E0"); System.out.println(eNotation1.format(123.456789)); My expected output is; 1.235E2 instead, 1.2346E2 was printed. Why was the output 1.2346E2? I…
Satellite Sage
  • 440
  • 1
  • 4
  • 13
1
2 3
43 44