10.01.2020
Posted by 
String To Double Java Average ratng: 7,5/10 7718 reviews
  1. Convert String To Double Java Example
  2. Convert String To Double Java
  3. Format Double To String Java

IntroductionIn this tutorial, we’ll take a look at the different ways to convert a double value to a String, removing its decimal places.We’ll look at how to do it when we want to just truncate the decimal part and when we want to round it. Truncation Using CastingIf our double value is within the int range, we can cast it to an int. The cast truncates the decimal part, meaning that it cuts it off without doing any rounding.This approach is about 10 times as fast as the other approaches we’ll look at.Once it’s an int, then we can then pass it to the valueOf method on the String class: String truncated = String.valueOf((int) doubleValue);We can confidently use this approach when we’re guaranteed that. But if our value exceeds that, casting won’t work like we’d want. Rounding Using String.formatNow, the remaining approaches aren’t as limited as casting, but they have their own nuances.For example, another approach is to use the format method of the String class. The first parameter of the method specifies that we are formatting a floating point value with zero digits after the decimal point: String rounded = String.format('%.0f', doubleValue);The format method uses HALFUP rounding which will round up if the value after the fractional part is.5 or above. Otherwise, it returns the number before the decimal point.And while simple, String.format is the slowest way to do this. Using NumberFormat.formatThe NumberFormat class also provides a format method similar to the String class, but NumberFormat is faster and with it, we can specify the rounding mode to achieve either truncation or rounding.The setMaximumFractionDigits method tells the formatter how many fractional digits after the decimal point to include in the output: NumberFormat nf = NumberFormat.getNumberInstance;nf.setMaximumFractionDigits(0);String rounded = nf.format(doubleValue);Curiously, NumberFormat doesn’t use HALFUP by default.

Java

Convert String To Double Java Example

Instead, it uses HALFEVEN rounding by default, meaning it will round like normal except at.5, in which case it will pick the nearest even number.While HALFEVEN is helpful with statistical analysis, let’s use HALFUP to be consistent: nf.setRoundingMode(RoundingMode.HALFUP);String rounded = nf.format(doubleValue);And, we can change this and achieve truncation by setting the formatter to use the FLOOR rounding mode instead: nf.setRoundingMode(RoundingMode.FLOOR);String truncated = nf.format(doubleValue)And now, it will truncate instead of round. Using DecimalFormat.formatSimilar to NumberFormat, the DecimalFormat class can be used to format double values.

Java

Convert String To Double Java

However, instead of setting the output format with method calls, we can tell the formatter what output we want by providing the constructor with a specific pattern: DecimalFormat df = new DecimalFormat('#,###');df.setRoundingMode(RoundingMode.HALFUP);String rounded = df.format(doubleValue);The “#,###” pattern signifies that we want the formatter to only return the integer part of the input. It also signals that we want the digits grouped in threes separated by a comma.The same rounding defaults apply here so if we want to output a truncated value we can set the rounding mode to FLOOR: df.setRoundingMode(RoundingMode.FLOOR);String truncated = df.format(doubleValue) 6. Using BigDecimal.toStringThe last approach we’ll look at is BigDecimal, which we’ll include because it out-performs NumberFormat and DecimalFormat for larger doubles.We can use BigDecimal‘s setScale method to tell whether we want to round or truncate: double largeDouble = 345345345345.56;BigDecimal big = new BigDecimal(largeDouble);big = big.setScale(0, RoundingMode.HALFUP);Remember that BigDecimals are immutable so, like Strings, we need to reset the value.And, then, we just call BigDecimal‘s toString: String rounded = big.toString; 7.

Format Double To String Java

ConclusionIn this tutorial, we looked at the different ways in which we can convert a double to a String while removing decimal places. We provided approaches that would output either rounded or truncated values.As usual, the samples and benchmarks are available.