GIDForums  

Go Back   GIDForums > Computer Programming Forums > MySQL / PHP Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 08-Oct-2004, 00:33
rose rose is offline
New Member
 
Join Date: Jul 2004
Posts: 5
rose is on a distinguished road

problem with rounding the float numbers


I have got problem with rounding the floating number when using round() function as well as numberformat
for eg:number_format( 424.025, 2, '.', ',' ) = 4.02 where it should be 424.03
also round(424.025,2) giving the same answer ie 4.02 what to do solve this problem.please help
  #2  
Old 08-Oct-2004, 06:07
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough
Hello rose,

The question I'd really like to ask here is whether you require this "result" just for display purposes (on a web page)?

If yes, then you may try using a custom function like the example in the PHP code below...

PHP Code:

<?php

function make_display_float( $float, $precision )
{
  // $float is really a float value here!
  // e.g. $float === 3.0249999999;
  $float = round( $float, $precision+1 );
  return( round($float, $precision) );
}

// say you have a real float number,
$total = floatval( 424.0249999 );

// to see the different results on a web page, test the following

  // with round()
  $ttl_after_round = round( $total, 2 );
  // with custom function
  $ttl_for_display = make_display_float( $total, 2 );

// echo the results
echo "\$ttl_after_round is $ttl_after_round<br />\n";
echo "\$ttl_for_display is $ttl_for_display";

?>


results in:
$ttl_after_round is 424.02
$ttl_for_display is 424.03

Please note that I am just suggesting this as a possible fix for your specific problem... I had to think this up just now, so I may not have thought this through enough. Your comments are definitely appreciated.
  #3  
Old 13-Oct-2004, 03:43
rose rose is offline
New Member
 
Join Date: Jul 2004
Posts: 5
rose is on a distinguished road
The function was very usefull.I implemented it and got the desired result.

thankyou verymuch,
rose
 
 

Recent GIDBlogToyota - 2008 July Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help with my programs, please help. agentxx04 C Programming Language 1 23-Sep-2004 18:02
Please Help, problems writing c program REVISED soulfly C Programming Language 6 03-Mar-2004 13:57
Code problem (a newbie question) monkster87 CPP / C++ Forum 3 11-Aug-2003 12:46

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 00:57.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.