Theodore K.
Theodore K.

Reputation: 5176

Remove digit mark from float and convert to integer

Let's say that I have these variables:

float xx= 99.33f;
int yy=0;

I want to convert xx to yy by removing the digit marks, so that yy will be the number 9933, preferably with a single line statement. How is this possible?

Upvotes: 0

Views: 155

Answers (3)

Maroun
Maroun

Reputation: 95988

Simple solution:

yy = (int) (xx * 100);

Upvotes: 1

Vinayak Pingale
Vinayak Pingale

Reputation: 1315

Try using this. its two line but, it might work.

float xx= (float) 99.33;
String yys=String.valueOf(xx).replace(".", "");
int yy=Integer.parseInt(yys);

Upvotes: 3

Donvino
Donvino

Reputation: 2575

Just the easiest way that comes up for me, but I assume that there could be easier ways to do that. But you can make it like a function.

String s = Float.toString(xx);
String[] temp=s.split("\\.");
yy=Integer.parseInt(temp[0]+temp[1]);

Upvotes: 3

Related Questions