deemru / WavesKit

Waves Platform Development Kit for PHP
https://packagist.org/packages/deemru/waveskit
MIT License
24 stars 8 forks source link

Please help how to convert price in Waves with PHP #3

Closed arnezt closed 5 years ago

arnezt commented 5 years ago

Hello sir.. I still confuse how to convert the prices on Waves with PHP. I have use the code like something below:


function convert_price($n)
{
    // first strip any formatting;
    $n = (0 + str_replace(",", "", $n));

    // is this a number?
    if (!is_numeric($n)) return false;
    // now filter it;
    if ($n > 10000000000000) return ($n / pow(10, 21)); //13/21
    elseif ($n > 1000000000000) return ($n / pow(10, 14)); //12/21
    elseif ($n > 100000000000) return ($n / pow(10, 14)); //11/21
    elseif ($n > 10000000000) return ($n / pow(10, 14)); //10/14
    elseif ($n > 1000000000) return ($n / pow(10, 13)); //9/13
    elseif ($n > 100000000) return ($n / pow(10, 13)); //8/13
    elseif ($n > 10000000) return ($n / pow(10, 8)); //7/13
    elseif ($n > 1000000) return ($n / pow(10, 8)); //6/10
    elseif ($n > 100000) return ($n / pow(10, 8)); //5/8
    elseif ($n > 10000) return ($n / pow(10, 8)); //4/8
    elseif ($n > 1000) return ($n / pow(10, 8)); //3/8
    elseif ($n > 100) return ($n / pow(10, 8)); //2/8
    elseif ($n > 10) return ($n / pow(10, 8)); //1/8
    elseif ($n > 0) return ($n / pow(10, 8)); //0/8

    return number_format($n, 8, ',', '.');
}```

but sometimes the formula above works, sometimes the digits numbers of price changes. I found the formula at the matcher.wavesplatform.com something like this:
```10^(8+Asset1desimal - Asset2desimal)```
and another formula with for Python.
But how to implement it to PHP? Thanks before if you kindly to make any function for this issue.
deemru commented 5 years ago

If you want to show a number you can use this variant from w8io project:

function w8io_amount( $amount, $decimals, $pad = 20 )
{
    if( $amount < 0 )
    {
        $sign = '-';
        $amount = -$amount;
    }
    else
        $sign = '';
    $amount = (string)$amount;
    if( $decimals )
    {
        if( strlen( $amount ) <= $decimals )
            $amount = str_pad( $amount, $decimals + 1, '0', STR_PAD_LEFT );
        $amount = substr_replace( $amount, '.', -$decimals, 0 );
    }
    $amount = $sign . $amount;
    return $pad ? str_pad( $amount, $pad, ' ', STR_PAD_LEFT ) : $amount;
}
arnezt commented 5 years ago

All right. Thank you very much for your kind to response. I'll try your function.