A Conversation for The H2G2 Programmers' Corner

Java NumberFormat

Post 1

Researcher PSG

Hello

I'm a bit new to Java and I was just wondering if there was a way to use NumberFormatter to format a text field to a float number.

Any help appreciatedsmiley - smiley

Researcher PSG


Java NumberFormat

Post 2

Calculator Nerd 256

i think it is called parseFloat(String);
i'm new to java, so i understand how annoying it is to learn new standards
must...perfect...anaglyph...class
if u haven't guessed, i'm having some trouble as well
if you need a huge database, try java.sun.com and somewhere on there is a really cool page full of classes and their methods and stuff
it's nice, but it won't tell you how to make your classes work right
(perhaps i'll start a thread about my anaglyph problem)
smiley - geek>8^B


Java NumberFormat

Post 3

Calculator Nerd 256

o
nm
i think i misunderstood u
u meant how to out a float

DecimalFormat myDecimalFormat = new DecimalFormat("0000.00");
System.out.println(myDecimalFormat.output(201.765));

that would make an output of "0201.77"
i think i got some of the words wrong, but that is basically how a DecimalFormat works
try it to make sure i am right before going and writing a big program with that in it
smiley - geek>8^B


Java NumberFormat

Post 4

Researcher PSG

Thanks Calc

I had found the DecimalFormat thing by accident a week or so ago, and I apologise for not posting it. But hopefully others will find it useful.

Why they couldn't have just had a float format thing though? oh well

Researcher PSG


Java NumberFormat

Post 5

Calculator Nerd 256

lol yeah

o i neglected to mention that u need this line at the beginning:
import java.text.DecimalFormat

now let's hope i can remember that by test time
smiley - geek>8^B


Java NumberFormat

Post 6

Calculator Nerd 256

oops
if u r copy/pasting, i left off the ; at the end of the line
smiley - geek>8^B


Java NumberFormat

Post 7

Calculator Nerd 256

oops again
(is there an tag? guess i'll find out when i post)
i put
DecimalFormat.output(float);
it should be
DecimalFormat.format(float);
maybe next time i'll just link to the javadoc
smiley - geek>8^B


Key: Complain about this post

Write an Entry

"The Hitchhiker's Guide to the Galaxy is a wholly remarkable book. It has been compiled and recompiled many times and under many different editorships. It contains contributions from countless numbers of travellers and researchers."

Write an entry
Read more