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: 68When
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
Find Difference Between Two Strings
How to Check Whether an Array Is Null/Empty
Setonclicklistener() on a Null Object Reference Error
Java.Lang.Noclassdeffounderror: Lorg/Apache/Logging/Log4J/Logger; But the Artifact Exists
How to Sort Integer Digits in Ascending Order Without Strings or Arrays
How to Valid @Requestheader in Spring Boot
Pdf to Byte Array and Vice Versa
How to Split a String, But Also Keep the Delimiters
In Java, Can You Modify a List While Iterating Through It
How to Move Marker Along Polyline Using Google Map
Java Spring: Best Way to Convert a File to a Multipartfile
How to Click an Li Inside a Ul Using Selenium Webdriver
Shifting Characters Within a String
Replace Whitespace in Json Keys
Extract Text Br Tags in Selenium Java
Crudrepository How to Find by Last Date Between
How to Tell a Mockito Mock Object to Return Something Different the Next Time It Is Called