﻿ How to Remove Trailing Zeros from a Double - ITCodar

How to Remove Trailing Zeros from a Double

How to remove trailing zeros from a double

Use DecimalFormat

``  double answer = 5.0;   DecimalFormat df = new DecimalFormat("###.#");  System.out.println(df.format(answer));``

Remove trailing zeros from double

If you are willing to switch to `BigDecimal`, there is a #stripTrailingZeroes() method that accomplishes this.

How to remove trailing zeros using Dart

I made regular expression pattern for that feature.

``double num = 12.50; // 12.5double num2 = 12.0; // 12double num3 = 1000; // 1000RegExp regex = RegExp(r'([.]*0)(?!.*\d)');String s = num.toString().replaceAll(regex, '');``

Swift - Remove Trailing Zeros From Double

In Swift 4 you can do it like that:

``extension Double {    func removeZerosFromEnd() -> String {        let formatter = NumberFormatter()        let number = NSNumber(value: self)        formatter.minimumFractionDigits = 0        formatter.maximumFractionDigits = 16 //maximum digits in Double after dot (maximum precision)        return String(formatter.string(from: number) ?? "")    }}``

example of use: `print (Double("128834.567891000").removeZerosFromEnd())`
result: 128834.567891

You can also count how many decimal digits has your string:

``import Foundationextension Double {    func removeZerosFromEnd() -> String {        let formatter = NumberFormatter()        let number = NSNumber(value: self)        formatter.minimumFractionDigits = 0        formatter.maximumFractionDigits = (self.components(separatedBy: ".").last)!.count        return String(formatter.string(from: number) ?? "")    }}``

How to remove trailing zeros when using doubles in strings with C#?

``string source = "2.4200";string output = double.Parse(source).ToString();``

Credits: here

Remove trailing zeros

Is it not as simple as this, if the input IS a string? You can use one of these:

``string.Format("{0:G29}", decimal.Parse("2.0044"))decimal.Parse("2.0044").ToString("G29")2.0m.ToString("G29")``

This should work for all input.

Update Check out the Standard Numeric Formats I've had to explicitly set the precision specifier to 29 as the docs clearly state:

However, if the number is a Decimal and the precision specifier is omitted, fixed-point notation is always used and trailing zeros are preserved

Watch out for values like 0.000001. G29 format will present them in the shortest possible way so it will switch to the exponential notation. `string.Format("{0:G29}", decimal.Parse("0.00000001",System.Globalization.CultureInfo.GetCultureInfo("en-US")))` will give "1E-08" as the result.

Remove trailing zero in C++

This is one thing that IMHO is overly complicated in C++. Anyway, you need to specify the desired format by setting properties on the output stream. For convenience a number of manipulators are defined.

In this case, you need to set `fixed` representation and set `precision` to 2 to obtain the rounding to 2 decimals after the point using the corresponding manipulators, see below (notice that `setprecision`causes rounding to the desired precision). The tricky part is then to remove trailing zeroes. As far as I know C++ does not support this out of the box, so you have to do some string manipulation.

To be able to do this, we will first "print" the value to a string and then manipulate that string before printing it:

``#include <iostream>#include <iomanip>int main(){     double value = 12.498;    // Print value to a string    std::stringstream ss;    ss << std::fixed << std::setprecision(2) << value;    std::string str = ss.str();    // Ensure that there is a decimal point somewhere (there should be)    if(str.find('.') != std::string::npos)    {        // Remove trailing zeroes        str = str.substr(0, str.find_last_not_of('0')+1);        // If the decimal point is now the last character, remove that as well        if(str.find('.') == str.size()-1)        {            str = str.substr(0, str.size()-1);        }    }    std::cout << str << std::endl;}``