1

Since I'm a beginner in PHP I can't figure it out how I should make my calculator display the zeros instead of -x at the end?

<?php
$intValue = 0;
$arrResult = json_decode(file_get_contents('http://preev.com/pulse/units:btc+usd/sources:bitfinex+bitstamp+btce'), true);
foreach($arrResult['btc']['usd'] as $key => $index){
    $intValue = $intValue + $arrResult['btc']['usd'][$key]['last'];
}
echo ($intValue / 3);
$val1 = 1;
$value001usd = $val1 / $intValue / 3 / 1000;
echo "  0.001 USD Equals to $value001usd BTC";
?>

It outputs the result at the time of this post as: 402.31333333333 0.001 USD Equals to 2.7618053369126E-7 BTC

Now I need it to be " 402.31333333333 0.001 USD Equals to 0.000002761 BTC"

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
J. Doe
  • 21
  • 2

2 Answers2

0

Use the function number_format() to properly format the value with the number of decimals you need:

$valueBTC = $val1 / $intValue / 3 / 1000;
$textBTC  = number_format($valueBTC, 10);
echo("  0.001 USD Equals to $textBTC BTC\n");
axiac
  • 68,258
  • 9
  • 99
  • 134
  • Copied Answer from my comment. LOL i have told you in my comment before his answer and you approved :P @J.Doe – K. Uzair Mar 05 '16 at 20:27
  • @K.Uzair I didn't even read the comments on your answer. I down-voted it and added my comment to explain why. And btw, the "solution" you posted in the comment doesn't answer the question. I can tell you without bothering to test it that `number_format($value001usd,0,'','') == '0'`. – axiac Mar 05 '16 at 20:32
-2

You Can Actually round your output number to whatever length you want. Here is an example echo round(1.9558334534534, 5); // Outputs 1.95583

Here is the link for php function where you can get extra help

http://php.net/manual/en/function.round.php

K. Uzair
  • 303
  • 3
  • 16
  • What about the zeros that need to be displayed instead of -7 at the end? – J. Doe Mar 05 '16 at 19:42
  • try using number format function. number_format($value001usd,0,'','') in your case. Hope it will solve your problem. you may look on this answer here. http://stackoverflow.com/questions/4964059/convert-exponential-to-a-whole-number-in-php – K. Uzair Mar 05 '16 at 19:50
  • Rounding doesn't change the value type. The value returned by `round()` is a double, `echo()` displays using the best format to show as much information (in this case decimals) it can. And for this number, the scientific format is the best. – axiac Mar 05 '16 at 20:10