'How to remove the decimal part from a float number that contains .0 in java
I just want to remove the fractional part of a float number that contains .0. All other numbers are acceptable.. For example :
I/P: 1.0, 2.2, 88.0, 3.56666, 4.1, 45.00 , 99.560
O/P: 1 , 2.2, 88, 3.567, 4.1, 45 , 99.560
Is there any method available to do that other than "comparing number with ".0" and taking substring" ?
EDIT : I don't want ACTING FLOAT NUMBERs (like 1.0, 2.0 is nothing but 1 2 right?)
I feel my question is little confusing...
Here is my clarification: I just want to display a series of floating point numbers to the user. If the fractional part of a number is zero, then display only the integer part, otherwise display the number as it is. I hope it's clear now..
Solution 1:[1]
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.
}
Solution 2:[2]
If android gives you lemons. Dont throw it.
YourFormatedStringNumber = NumberFormat.getInstance().format(myNumber);
makes -> "12.50" to "12.50" and "12.00" to "12"
Solution 3:[3]
Try:
String.format("%.0f",value);
Solution 4:[4]
I can't comment yet so just a heads up. number value needs to be a double for it to work.
String.format("%.0f", number)
Solution 5:[5]
try NumberFormat or even DecimalFormat:
If you want to not show a decimal point where there might be no digits after the decimal point, use setDecimalSeparatorAlwaysShown.
Solution 6:[6]
Well, technically the display part is text, so you could do it with regular expression. If the number matches then use groups to extract the correct part. (I'll update the post with a solution later.)
Alternatively, you could simply go:
if ((int)Math.round(number) == number) {
System.out.println((int) number)
} else {
System.out.println(number)
}
or even:
System.out.println((int) Math.round(number) == number ?
String.valueOf((int) number) : String.valueOf(number));
careful, the String.valueOf() is required, as this does not work. The System.out.println(double) will always get executed, i.e. the below does not work:
System.out.println((int)Math.round(number) == number ? (int) number : number);
Solution 7:[7]
try casting int before the float number
Solution 8:[8]
I've created this extension method. if amount contains decimal(40.01) it will show decimal other wise it will show only amount(40.00)
input 2000.43, Result = 2,000.43
input 2000.00, Result = 2,000
fun Double.FormattedAmount(): String { val otherSymbols = DecimalFormatSymbols(Locale.US) val decimalFormat = DecimalFormat("#,###,###.##", otherSymbols) return decimalFormat.format(this).toString() }
Solution 9:[9]
Very simple for kotlin
float number = 12.5
number.toInt()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | npinti |
Solution 2 | |
Solution 3 | Sam |
Solution 4 | thesamoanppprogrammer |
Solution 5 | |
Solution 6 | |
Solution 7 | Nizam Ahmed |
Solution 8 | Atif AbbAsi |
Solution 9 | donmj |