views:

964

answers:

2

Hi folks,

When calculating a golf handicap differential you are supposed to truncate the answer to 1 decimal place without rounding. No idea why but...

I know how to do this using TRUNCATE() in mySQL

 SELECT TRUNCATE( 2.365, 1 );
// outputs 2.3

but I was wondering if sprintf() could do this? The only way I know to work with decimal places in a float is ...

echo sprintf("%.1f", 2.365);
// outputs 2.4

TIA

JG

+3  A: 

What language is this in? Assuming it's C or one of its derivatives, and assuming you always want exactly one decimal place, and assuming your values are always non-negative, you can do this:

float val = 12.3456;
val = floor(val*10.0)/10.0;
sprintf("%.1f", val);

Is there a better way? Probably. This is just what comes to mind.

Darryl
Thanks! Works for me in PHP!
jerrygarciuh
A: 

here is the solution in ActionScript 3.0 which uses int type casting.

http://www.actionscript-flash-guru.com/blog/32-truncating-a-number--setting-the-precision-of-a-float--actionscript-3-as3

Niko Limpika