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 › Truncate Command
Page Index Toggle Pages: 1
Truncate Command? (Read 604 times)
Truncate Command?
Oct 24th, 2008, 8:02pm
 
Does Processing have a truncate command and if so what is it? I have a floating point number and I want to cut it down to just two digits after the decimal point. How do I do that please?
Re: Truncate Command?
Reply #1 - Oct 25th, 2008, 6:07pm
 
Maybe this?

float truncate(float x){
 if ( x > 0 )
   return float(floor(x * 100))/100;
 else
   return float(ceil(x * 100))/100;
}

Shifts the decimal point two spots to the right, floors (or ceils) it, effectively cutting off the post-decimal digits, shifts back.
Re: Truncate Command?
Reply #2 - Oct 26th, 2008, 1:14pm
 
Elegant solution. Thanks.
Page Index Toggle Pages: 1