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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

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.