1

I have to write a function to change a pricetype from (ex: 25400000) to 25 milion 400 thousand . I have a code but it just change when the price type is even (ex : 25000000) . Here is my code .

static public function priceFormat($number, $type ='vnd')
{
    $subfix = 'vnd';
    if ($type == 'vnd'){
        $temp = substr($number, -6, -1);
        if ($temp == '000000'){
            $number = str_replace("000000", '', $number);
            $subfix = 'million';
        }
    }
    while (true) {
        $replaced = preg_replace('/(-?\d+)(\d\d\d)/', '$1,$2', $number);
        if ($replaced != $number) {
            $number = $replaced;
        } else {
            break;
        }
    }
    if ($type != 'vnd')
        return $number.' '.$type;
    return $number.' '.$subfix;
}
Asamoa
  • 147
  • 2
  • 13
  • 1
    Does this answer help, http://stackoverflow.com/questions/10221694/convert-number-into-xx-xx-million-format/10221725#10221725 your welcome. – Lawrence Cherone Apr 27 '14 at 02:59
  • no, 14120000 it just return 14.12milion , but i want it return 14 milion and 120 thousand – Asamoa Apr 27 '14 at 03:04

1 Answers1

0

This code:

$number = 25400055;

$millionsRemainder = $number % 1000000;

$millions = ($number - $millionsRemainder) / 1000000;

$thousandsRemainder = $millionsRemainder % 1000;

$thousands = ($millionsRemainder - $thousandsRemainder) / 1000;

echo $millions . ' million ' . $thousands . ' thousands ' . ' and ' . $thousandsRemainder;

Should output "25 million 400 thousands and 55"

thexacre
  • 702
  • 4
  • 9