Re: how to stop variable rounding up after a division



Alvaro G. Vicario wrote:
*** mantrid escribió/wrote (Sat, 07 Oct 2006 18:36:03 GMT):
$price=number_format($price/100,2);
$price=number_format($price,2)/100;

80 becomese 1 instead of 0.8
180 becomes 2 instead of 1.8

In my system, this code:

<?php

echo number_format(80/100, 2) . "\n";
echo number_format(80, 2)/100 . "\n";

?>

prints this output:

0.80
0.8

:-?


You can try forcing a cast to float, that works in other languages:

(float)$foo
100.0



That makes sense. The second example is not formatting the fraction, so it is printed in default format.

Colin
.



Relevant Pages

  • Re: how to stop variable rounding up after a division
    ... You can try forcing a cast to float, ...
    (comp.lang.php)
  • Re: regarding unix specific commands
    ... know about to a variadic function is to cast it. ... The type must have a size less than or equal to the cast type ... C99 has solved these problems by defining a formatting modifier for each ... for my own printf implementation I introduced a formatting ...
    (comp.unix.programmer)
  • Re: How do I make mass format changes to mail merge fields?
    ... Are you in a position to create views in SQL Server that convert the float fields to formatted text fields, then use those views as the data sources for your merge? ...
    (microsoft.public.word.mailmerge.fields)
  • Re: Lining up with page edge.
    ... (The contextual selectors are only for ease of later ... > float: right; ... much less formatting stuff in the markup - in fact no formatting stuff ... in the markup - which is the way CSS is supposed to work. ...
    (comp.infosystems.www.authoring.stylesheets)
  • Re: How do I format a float number?
    ... If you do not want to return a string, then you don't need to do anything ... with formatting. ... A float is a 32-bit binary number. ... HTH, ...
    (microsoft.public.dotnet.framework)