14

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..

vnshetty
  • 20,051
  • 23
  • 64
  • 102
  • "Comparing number with ''.0'' and taking substring" won't convert "45.00" to "45". Do you want to strip an arbitrary number of zeroes from the end? – Mike Samuel May 08 '12 at 10:15
  • Have a look at this : http://stackoverflow.com/questions/703396/how-to-nicely-format-floating-types-to-string – Kazekage Gaara May 08 '12 at 10:22
  • Do you have to keep the trailing 0 on '99.560' or would '99.56' be OK? – NickT May 08 '12 at 10:49
  • Post some source code. Your question is meaningless without it. – user207421 May 08 '12 at 10:51
  • @ NickT it doesnt matter i only integer in integer format. i dont want ACTING FLOAT NUMBER (like 1.0, 2.0 is nothing but 1 2 right?) – vnshetty May 08 '12 at 10:52
  • @ EJP i dont want ACTING FLOAT NUMBERs (like 1.0, 2.0 is nothing but 1 2 right?) – vnshetty May 08 '12 at 10:53
  • 2
    I don't understand your comment. What I meant was that for all the i/ps and required o/ps you have listed, then IF for an i/p of 99.560 an o/p of 99.56 was acceptable, then a simple NumberFormat formatter = new DecimalFormat("###.###"), would give you what you require. (all other o/ps would be as you have listed them) – NickT May 08 '12 at 10:58
  • @vnshetty: In what way does your statement constitute posting source code? – user207421 May 08 '12 at 11:05

9 Answers9

18

If android gives you lemons. Dont throw it.

YourFormatedStringNumber = NumberFormat.getInstance().format(myNumber);

makes -> "12.50" to "12.50"  and "12.00" to "12"

More here in doc.

amalBit
  • 12,041
  • 6
  • 77
  • 94
15

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.
}
npinti
  • 51,780
  • 5
  • 72
  • 96
  • thanks for that, fast and efficient ^^ Under Android i did Float remains = floatval - FloatMath.floor(floatval); – Darkendorf Nov 08 '12 at 07:48
13

Try:

String.format("%.0f",value);
Sam
  • 446
  • 7
  • 17
3

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)
Mr. Disability
  • 799
  • 1
  • 10
  • 19
2

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.

Roman K
  • 3,309
  • 1
  • 33
  • 49
0

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);
Jaco Van Niekerk
  • 4,180
  • 2
  • 21
  • 48
0

try casting int before the float number

0

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)

  1. input 2000.43, Result = 2,000.43

  2. input 2000.00, Result = 2,000

     fun Double.FormattedAmount(): String {
    
        val otherSymbols = DecimalFormatSymbols(Locale.US)
        val decimalFormat = DecimalFormat("#,###,###.##", otherSymbols)
        return decimalFormat.format(this).toString()
    
     }
    
Atif AbbAsi
  • 5,633
  • 7
  • 26
  • 47
0

Very simple for kotlin

float number = 12.5
number.toInt()
donmj
  • 379
  • 7
  • 13