Simple and clean java float to string conversion -


this simple question, i'm amazed on how difficult has been answer. documentation didn't give clear , straight answer.

you see, i'm trying convert simple float string such result has 1 decimal digit. example:

string myfloatstring = ""; float myfloat = 33.33; myfloatstring = float.tostring(myfloat); 

this indeed return "33.33". fine, except i'm looking way truncate decimal "33.3". needs impose decimal whenever whole number put in. if myfloat 10, example, need display "10.0". oddly not simple sounds.

any advice can give me appreciated.

edit #1:

for moment, i'm not concerned digits left of decimal point.

system.out.println(string.format("%.1g%n", 0.9425)); system.out.println(string.format("%.1g%n", 0.9525)); system.out.println(string.format( "%.1f", 10.9125)); 

returns:

0.9 1 10.9 

use third example case


Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

oracle - Changing start date for system jobs related to automatic statistics collections in 11g -