We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › printf alternative
Page Index Toggle Pages: 1
printf alternative (Read 1234 times)
printf alternative
Dec 6th, 2009, 12:21am
 
hi i have serial input which i need to convert to float.
the docs mention converting using printf/scanf to format
can println do this? and where is the parseFloat documentation?

i currntly use
Code:

println(myString);
String[] list = trim(split(myString, ','));
for(int iii=1;iii<8;iii++){
println(" <var"+iii+": "+Float.parseFloat(list[iii]));
}

printf/scanf | example
%+07.2f | +082.76
%+09.6f | +0.053362
%+07.4f | -0.3647
%+07.3f | -09.091
%+08.4f | +00.0017
%+13.6E | +9.801470E-01
%+13.6E | +8.135935E-02
%+13.6E | +4.968087E-06
Re: printf alternative
Reply #1 - Dec 6th, 2009, 2:37am
 
"the docs mention converting using printf/scanf to format"
Which docs That's (typically) C/C++ functions, not Java. And scanf won't format, it parses formatted text.

Processing has nf() functions and friends.
For more precision, Java has the Formatter class, equivalent to printf, accessible via String.format method.
Re: printf alternative
Reply #2 - Dec 6th, 2009, 4:40am
 
Actually Java, and therefore Processing, has had printf() for a while now (since 1.5). It can be used exactly the same way as its C/C++ counterparts. See Java's printf() documentation.

This works perfectly well in Processing if you prefer it to nf() or Formatter, for example,

Quote:
float number = 123.456789;

System.out.printf("%7.2f",number);


Having said that, if you are converting input rather than outputting to the screen or other stream, you may be better off with the alternatives that PhiLho suggested.

Page Index Toggle Pages: 1