Jump to content
imgrooot

Is there a way to convert Bitcoin to Satoshie and vice versa?

Recommended Posts

Apparently it's bad to use floats when working with bitcoin. But the API i'm using doesn't really give an option to convert Bitcoin to Satoshie or vice versa. https://www.block.io/api/simple/php

So I'm trying to figure out a way to do that function with PHP.

Here is the code I found that converts from Satoshie to Bitcoin.  My question is, how do I convert from Bitcoin to Satoshie.

function convertToBTCFromSatoshi($value) {
    $BTC = $value / 100000000 ;
    return $BTC;
}
function formatBTC($value) {
    $value = sprintf('%.8f', $value);
    $value = rtrim($value, '0') . ' BTC';
    return $value;
}
echo formatBTC(convertToBTCFromSatoshi(5000));

 

Share this post


Link to post
Share on other sites

This is what converts.

function convertToBTCFromSatoshi($value) {
    $BTC = $value / 100000000 ;
    return $BTC;
}

Do I need to say anything more?

Share this post


Link to post
Share on other sites

I may regret asking this but - are you sure about that?  Is going from a to b involves division, wouldn't the reverse involve multiplication?

Share this post


Link to post
Share on other sites
2 hours ago, requinix said:

This is what converts.


function convertToBTCFromSatoshi($value) {
    $BTC = $value / 100000000 ;
    return $BTC;
}

Do I need to say anything more?

 

2 hours ago, ginerjm said:

I may regret asking this but - are you sure about that?  Is going from a to b involves division, wouldn't the reverse involve multiplication?

 

Correct, the multiplication would do the reverse. Like this. 

function convertToSatoshi($value) {
    $SAT = $value * 100000000 ;
    return $SAT;
}

 

I was more worried about reversing the decimal part of the 2nd function, but I just realized that it's not needed to convert to Satoshie.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.