java - How to check the precision of Float -
i have float
one:
float f = float.valueof("9.222333999444555666"); system.out.println(f); //prints 9.222334
is there way count number of digits after .
sign without using regex: f.tostring().split("\\.")[1].length()
?
simply convert value string , place of decimal point.
string s = "" + f; int = s.indexof(".") system.out.println("the number of digits after decimal point " + s.length()-i-1
edit: read op's comment , doubt how round off float. question answered here: format float n decimal places
for quick reference, use this: string.format("%.2f", f). have understand java's float , double not have methods , attributes manipulating precision after decimal point. there several reasons java this, should remember convert float , double string, manipulate them want , convert back.
Comments
Post a Comment