Menu

#29 ToCurrency() won't handle negative values

open
nobody
None
5
2008-12-22
2008-12-22
No

Hi,
Passing a negative value to ToCurrency() will have weird results. Furthermore it adds zeros even in front of numbers below 100. That looks really strange. Last but not least it will ignore locale settings and produce only anglo-american output.

Currently I use the standard number instead of currency to get arround these problems.

I haven't tested it yet, but the function ToMetric() seems to have the same problem. Furthermore the unit multipliers are wrong:
g didn't exist, I think you mean G=Giga (*1.000.000.000)
m means milli (1/1000) but I think you meant M=Mega (*1.000.000)

Best Regards
Matthias

Discussion

  • Nobody/Anonymous

    HI, this could help to solve your first problem - but the problem with negative values remain.

    Look for this method in pChart.class:

    function ToCurrency($Value)
    {
    $Go = floor($Value/1000000000);
    $Mo = floor(($Value - $Go*1000000000)/1000000);
    $Ko = floor(($Value - $Go*1000000000 - $Mo*1000000)/1000);
    $o = floor($Value - $Go*1000000000 - $Mo*1000000 - $Ko*1000);

    // Modified START
    if ($Value>=1000) {
    if ( strlen($o) == 1 ) { $o = "00".$o; }
    if ( strlen($o) == 2 ) { $o = "0".$o; }
    }

    if ( strlen($Ko) == 1 && $Mo!=0 ) { $Ko = "00".$Ko; }
    if ( strlen($Ko) == 2 && Mo!=0) { $Mo = "0".$Ko; }

    if ( strlen($Mo) == 1 && $Go!=0 ) { $Mo = "00".$Mo; }
    if ( strlen($Mo) == 2 && $Go!=0 ) { $Mo = "0".$Mo; }

    $ResultString = $o;
    if ( $Ko != 0 || $Mo != 0 || $Ko != 0 ) { $ResultString = $Ko.".".$ResultString; }
    if ( $Mo != 0 || $Go != 0 ) { $ResultString = $Mo.".".$ResultString; }
    if ( $Go != 0 ) { $ResultString = $Go.".".$ResultString; }

    // Modified END

    $ResultString = $this->Currency.$ResultString;
    return($ResultString);
    }

     
  • Nobody/Anonymous

    SO, i now got a solution for both of your problems.

    Try this:

    /* Convert to curency */
    function ToCurrency($Value)
    {
    if ($Value<0) $isnegative=true;
    $Value=abs($Value);

    $Go = floor($Value/1000000000);
    $Mo = floor(($Value - $Go*1000000000)/1000000);
    $Ko = floor(($Value - $Go*1000000000 - $Mo*1000000)/1000);
    $o = floor($Value - $Go*1000000000 - $Mo*1000000 - $Ko*1000);

    if ($Value>=1000) {
    if ( strlen($o) == 1 ) { $o = "00".$o; }
    if ( strlen($o) == 2 ) { $o = "0".$o; }
    }

    if ( strlen($Ko) == 1 && $Mo!=0 ) { $Ko = "00".$Ko; }
    if ( strlen($Ko) == 2 && Mo!=0) { $Mo = "0".$Ko; }

    if ( strlen($Mo) == 1 && $Go!=0 ) { $Mo = "00".$Mo; }
    if ( strlen($Mo) == 2 && $Go!=0 ) { $Mo = "0".$Mo; }

    $ResultString = $o;
    if ( $Ko != 0 || $Mo != 0 || $Go != 0 ) { $ResultString = $Ko.".".$ResultString; }
    if ( $Mo != 0 || $Go != 0 ) { $ResultString = $Mo.".".$ResultString; }
    if ( $Go != 0 ) { $ResultString = $Go.".".$ResultString; }

    if ($isnegative) $ResultString="-".$ResultString;

    $ResultString = $this->Currency.$ResultString;
    return($ResultString);
    }

    Greets from germany Bernie78

     

Log in to post a comment.

MongoDB Logo MongoDB