|
1 | 1 | package com.baeldung.decimalformat; |
2 | 2 |
|
3 | | - import java.math.RoundingMode; |
4 | | - import java.text.DecimalFormat; |
5 | | - import java.text.NumberFormat; |
6 | | - import java.util.Locale; |
| 3 | +import java.math.RoundingMode; |
| 4 | +import java.text.DecimalFormat; |
| 5 | +import java.text.NumberFormat; |
7 | 6 |
|
8 | | - public class DoubletoString { |
| 7 | +public class DoubletoString { |
9 | 8 |
|
10 | | - public static void main(String[] args) { |
| 9 | + public static void main(String[] args) { |
11 | 10 |
|
12 | | - double doubleValue = 345.56; |
| 11 | + double doubleValue = 345.56; |
13 | 12 |
|
14 | | - System.out.println(String.valueOf((int) doubleValue)); |
| 13 | + System.out.println(String.valueOf((int) doubleValue)); |
15 | 14 |
|
16 | | - System.out.println(String.format("%.0f", doubleValue)); |
| 15 | + System.out.println(String.format("%.0f", doubleValue)); |
17 | 16 |
|
18 | | - doubleValue = Math.floor(doubleValue); |
19 | | - DecimalFormat df = new DecimalFormat("#"); |
20 | | - df.setRoundingMode(RoundingMode.FLOOR); |
21 | | - System.out.println(df.format(doubleValue)); |
22 | | - |
23 | | - Locale enlocale = new Locale("en", "US"); |
24 | | - String pattern = "###,##"; |
25 | | - df = (DecimalFormat) NumberFormat.getNumberInstance(enlocale); |
26 | | - df.applyPattern(pattern); |
27 | | - String format = df.format(doubleValue); |
28 | | - System.out.println(format); |
| 17 | + NumberFormat nf = NumberFormat.getInstance(); |
| 18 | + nf.setMaximumFractionDigits(0); |
| 19 | + nf.setRoundingMode(RoundingMode.FLOOR); |
| 20 | + System.out.println(nf.format(doubleValue)); |
29 | 21 |
|
30 | | - Locale dalocale = new Locale("da", "DK"); |
31 | | - df = (DecimalFormat) NumberFormat.getNumberInstance(dalocale); |
32 | | - df.applyPattern(pattern); |
33 | | - System.out.println(df.format(doubleValue)); |
| 22 | + doubleValue = Math.floor(doubleValue); |
| 23 | + DecimalFormat df = new DecimalFormat("#,###"); |
| 24 | + df.setRoundingMode(RoundingMode.FLOOR); |
| 25 | + System.out.println(df.format(doubleValue)); |
34 | 26 |
|
| 27 | + } |
35 | 28 |
|
36 | | - } |
37 | | - |
38 | | - } |
| 29 | +} |
0 commit comments