Welcome Guest, Not a member yet? Register   Sign In
Format decimal removing anything before the decimal point
#4

[eluser]TheFuzzy0ne[/eluser]
Multiplying it by 100 will shift the decimal place over to the right by two spaces. This won't work, however, if you have a number such as 0.376, which will generate 37.6.

Another solution might be to just use a regular expression. Something like:

Code:
$number = 0.37;
$pattern = '.+\.([0-9]+)';
if (eregi($pattern, $number, $matches)) {
    $number = $matches[1];
}
echo $number;

this will work no matter how many decimal places you have, however, if you are interested in only the first 2 decimal places, multiplying by 100, and then using floor() should work for you. Like this:

Code:
$number = 0.37;
$number = floor($number*100);
EDIT: You can also use ceil() or round(), depending on what exactly it is you need.


Messages In This Thread
Format decimal removing anything before the decimal point - by El Forum - 05-01-2008, 08:46 AM



Theme © iAndrew 2016 - Forum software by © MyBB