How to Remove the Decimal Part If It Is Only 0

How to remove the decimal part if it is only 0?

You can cast it to an int and compare it with the double value. If the comparison results in true then you have to use the converted value or else continue using the double.

Do something like this,

private String getResultValue(double ret) {
int convertedValue = (int) ret;
return String.valueOf(ret == convertedValue ? convertedValue : ret);
}

For Example,

  • When ret = 68.0, convertedValue = 68 --> Comparison: True,
    Returned Value: 68

  • When ret = 68.4, convertedValue = 68 --> Comparison: False, Returned Value: 68.4

Remove useless zero digits from decimals in PHP

$num + 0 does the trick.

echo 125.00 + 0; // 125
echo '125.00' + 0; // 125
echo 966.70 + 0; // 966.7

Internally, this is equivalent to casting to float with (float)$num or floatval($num) but I find it simpler.

Javascript: Remove last decimal if zero

You could check the last digit of a stringed number and remove the last zero, if exists.

console.log([1.5, 1.49, 1.579].map(v => {    var temp = v.toFixed(3);    return temp.slice(-1) === '0'        ? temp.slice(0, -1)        : temp;}));

Swift - How to remove a decimal from a float if the decimal is equal to 0?

Swift 3/4:

var distanceFloat1: Float = 5.0
var distanceFloat2: Float = 5.540
var distanceFloat3: Float = 5.03

extension Float {
var clean: String {
return self.truncatingRemainder(dividingBy: 1) == 0 ? String(format: "%.0f", self) : String(self)
}
}

print("Value \(distanceFloat1.clean)") // 5
print("Value \(distanceFloat2.clean)") // 5.54
print("Value \(distanceFloat3.clean)") // 5.03

Swift 2 (Original answer)

let distanceFloat: Float = (currentUser.distance! as NSString).floatValue
distanceLabel.text = String(format: distanceFloat == floor(distanceFloat) ? “%.0f" : "%.1f", distanceFloat) + "Km"

Or as an extension:

extension Float {
var clean: String {
return self % 1 == 0 ? String(format: "%.0f", self) : String(self)
}
}

How to remove the decimal part from a float number that contains .0 in java

You could use a regular expression such as this: \\.0+$. If there are only 0's after the decimal point this regular expression will yield true.

Another thing you could do is something like so:

float x = 12.5;
float result = x - (int)x;
if (result != 0)
{
//If the value of `result` is not equal to zero, then, you have a decimal portion which is not equal to 0.
}

How can I remove the decimal part from JavaScript number?

You could use...

  • Math.trunc() (truncate fractional part, also see below)
  • Math.floor() (round down)
  • Math.ceil() (round up)
  • Math.round() (round to nearest integer)

...dependent on how you wanted to remove the decimal.

Math.trunc() isn't supported on all platforms yet (namely IE), but you could easily use a polyfill in the meantime.

Another method of truncating the fractional portion with excellent platform support is by using a bitwise operator (.e.g |0). The side-effect of using a bitwise operator on a number is it will treat its operand as a signed 32bit integer, therefore removing the fractional component. Keep in mind this will also mangle numbers larger than 32 bits.


You may also be talking about the inaccuracy of decimal rounding with floating point arithmetic.

Required Reading - What Every Computer Scientist Should Know About Floating-Point Arithmetic.

How to remove the decimals in case if they are zero?

You can check if the number has decimal places or not and generate appropriate result.

public static string MyDoubleToString(double d)
{
// preventing rounding
// if you want 5.9999 becomes 6 then comment the line below
d = Math.Truncate(d * 100) / 100;

return $"{d.ToString("f2")}%".Replace(".00%", "%");
}

You can use it like this.

var doubles = new double[] { 5.0, 5.999, 3.2 };

foreach (var d in doubles)
Console.WriteLine(MyDoubleToString(d));

and result will be

5%

5.99%

3.20%

If you want to use it in razor then

@MyDoubleToString(item.percentage)


Related Topics



Leave a reply



Submit