From 6009c4f9100078b2fb347d665e08a964c0572a8f Mon Sep 17 00:00:00 2001 From: daoire Date: Wed, 12 Sep 2018 20:35:59 +0100 Subject: [PATCH] Updates to Double to string --- .../decimalformat/DoubletoString.java | 45 ++++++++----------- 1 file changed, 18 insertions(+), 27 deletions(-) diff --git a/core-java/src/main/java/com/baeldung/decimalformat/DoubletoString.java b/core-java/src/main/java/com/baeldung/decimalformat/DoubletoString.java index 87d10a3548..e605c5e200 100644 --- a/core-java/src/main/java/com/baeldung/decimalformat/DoubletoString.java +++ b/core-java/src/main/java/com/baeldung/decimalformat/DoubletoString.java @@ -1,38 +1,29 @@ package com.baeldung.decimalformat; - import java.math.RoundingMode; - import java.text.DecimalFormat; - import java.text.NumberFormat; - import java.util.Locale; +import java.math.RoundingMode; +import java.text.DecimalFormat; +import java.text.NumberFormat; - public class DoubletoString { +public class DoubletoString { - public static void main(String[] args) { + public static void main(String[] args) { - double doubleValue = 345.56; + double doubleValue = 345.56; - System.out.println(String.valueOf((int) doubleValue)); + System.out.println(String.valueOf((int) doubleValue)); - System.out.println(String.format("%.0f", doubleValue)); + System.out.println(String.format("%.0f", doubleValue)); - doubleValue = Math.floor(doubleValue); - DecimalFormat df = new DecimalFormat("#"); - df.setRoundingMode(RoundingMode.FLOOR); - System.out.println(df.format(doubleValue)); - - Locale enlocale = new Locale("en", "US"); - String pattern = "###,##"; - df = (DecimalFormat) NumberFormat.getNumberInstance(enlocale); - df.applyPattern(pattern); - String format = df.format(doubleValue); - System.out.println(format); + NumberFormat nf = NumberFormat.getInstance(); + nf.setMaximumFractionDigits(0); + nf.setRoundingMode(RoundingMode.FLOOR); + System.out.println(nf.format(doubleValue)); - Locale dalocale = new Locale("da", "DK"); - df = (DecimalFormat) NumberFormat.getNumberInstance(dalocale); - df.applyPattern(pattern); - System.out.println(df.format(doubleValue)); + doubleValue = Math.floor(doubleValue); + DecimalFormat df = new DecimalFormat("#,###"); + df.setRoundingMode(RoundingMode.FLOOR); + System.out.println(df.format(doubleValue)); + } - } - - } +}