jeojavi
jeojavi

Reputation: 886

Show only decimals of float

I need to print a float showing only the decimals of that float. For example:

1.23456 --> 23456
12.3456 --> 3456
123.456 --> 456

I've found the following solution:

float floatValue = 1.23455f;
String stringValue = Float.toString(floatValue);
int pointIndex = stringValue.indexOf(".");
String decimals = stringValue.substring(pointIndex + 1, stringValue.length() - 1);

But I think it's a little dirty and I wonder if there is any other standard way, using String.format or something similar. I did't find anything in the documentation. Thanks in advance!

Upvotes: 0

Views: 168

Answers (1)

PKlumpp
PKlumpp

Reputation: 5233

float floatValue = 1.23455f;
String stringValue = Float.toString(floatValue).split("\\.")[1];

Should work for you

Upvotes: 1

Related Questions