This is what i have came up with thanks to brenns10.
std::string strUSD = "124.19129999";
double fUSD = std::stod(strUSD);
uint64_t uiCents = USD2CENTS(fUSD);
double fUSDBack = CENTS2USD(uiCents);
printf("USD / CENTS\n");
printf("(string)%s = (double)%lf = (uint cents)%llu\n", strUSD.data(), fUSD, uiCents);
printf("Present: %.2fUSD\n", fUSDBack);
printf("\n");
std::string strBTC = "1.12345678";
double fBTC = std::stod(strBTC);
uint64_t uiSatoshi = BTC2SATOSHI(fBTC);
double fBTCBack = SATOSHI2BTC(uiSatoshi);
printf("BTC / SATOSHI\n");
printf("(string)%s = (double)%lf = (uint satoshi)%llu\n",strBTC.data(),fBTC,uiSatoshi);
printf("Present: %.8fBTC\n", fBTCBack);
// Convert BITCOIN to SATOSHIs
uint64_t CSystemUtil::BTC2SATOSHI(const double& value)
{
return static_cast<uint64_t>(value * 1e8 + (value < 0.0 ? -.5 : .5));
}
// Convert SATOSHIS to BITCOIN
double CSystemUtil::SATOSHI2BTC(const uint64_t& value)
{
return static_cast<double>(value/1e8);
}
// Convert CENT to USD
double CSystemUtil::CENTS2USD(const uint64_t& value)
{
return static_cast<double>(value)/100;
}
// Convert USD to CENT
uint64_t CSystemUtil::USD2CENTS(const double& value)
{
return static_cast<uint64_t>(value * 100);
}
Output:
USD => CENTS => USD (string)124.19129999 = (double)124.191300 = (uint
cents)12419 Present: 124.19USD
BTC => SATOSHI => BTC (string)1.12345678 = (double)1.123457 = (uint
satoshi)112345678 Present: 1.12345678BTC
Worth notice, look at the printf showing the double values.