Skip to content

Commit c65be06

Browse files
author
minium
committed
Fixed double imprecisionness issue.
1 parent c007531 commit c65be06

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed

src/bitcoinapi/bitcoinapi.cpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include <string>
1414
#include <stdexcept>
15+
#include <cmath>
1516

1617
#include <jsonrpccpp/client.h>
1718
#include <jsonrpccpp/client/connectors/httpclient.h>
@@ -55,6 +56,11 @@ int BitcoinAPI::StringToNumber (const string &text){
5556
return ss >> result ? result : 0;
5657
}
5758

59+
double BitcoinAPI::RoundDouble(double num)
60+
{
61+
return floor(num * pow(10,8)) / pow(10,8);
62+
}
63+
5864
Value BitcoinAPI::sendcommand(const string& command, const Value& params){
5965
Value result;
6066

@@ -372,7 +378,7 @@ void BitcoinAPI::keypoolrefill() {
372378
bool BitcoinAPI::settxfee(double amount) {
373379
string command = "settxfee";
374380
Value params, result;
375-
params.append(amount);
381+
params.append(RoundDouble(amount));
376382
result = sendcommand(command, params);
377383
return result.asBool();
378384
}
@@ -701,7 +707,7 @@ bool BitcoinAPI::move(const string& fromaccount, const string& toaccount, double
701707

702708
params.append(fromaccount);
703709
params.append(toaccount);
704-
params.append(amount);
710+
params.append(RoundDouble(amount));
705711
params.append(minconf);
706712
result = sendcommand(command, params);
707713

@@ -714,7 +720,7 @@ bool BitcoinAPI::move(const string& fromaccount, const string& toaccount, double
714720

715721
params.append(fromaccount);
716722
params.append(toaccount);
717-
params.append(amount);
723+
params.append(RoundDouble(amount));
718724
params.append(minconf);
719725
params.append(comment);
720726
result = sendcommand(command, params);
@@ -737,7 +743,7 @@ string BitcoinAPI::sendtoaddress(const string& bitcoinaddress, double amount) {
737743
Value params, result;
738744

739745
params.append(bitcoinaddress);
740-
params.append(amount);
746+
params.append(RoundDouble(amount));
741747

742748
result = sendcommand(command, params);
743749
return result.asString();
@@ -748,7 +754,7 @@ string BitcoinAPI::sendtoaddress(const string& bitcoinaddress, double amount, co
748754
Value params, result;
749755

750756
params.append(bitcoinaddress);
751-
params.append(amount);
757+
params.append(RoundDouble(amount));
752758
params.append(comment);
753759
params.append(comment_to);
754760

@@ -762,7 +768,7 @@ string BitcoinAPI::sendfrom(const string& fromaccount, const string& tobitcoinad
762768

763769
params.append(fromaccount);
764770
params.append(tobitcoinaddress);
765-
params.append(amount);
771+
params.append(RoundDouble(amount));
766772

767773
result = sendcommand(command, params);
768774
return result.asString();
@@ -774,7 +780,7 @@ string BitcoinAPI::sendfrom(const string& fromaccount, const string& tobitcoinad
774780

775781
params.append(fromaccount);
776782
params.append(tobitcoinaddress);
777-
params.append(amount);
783+
params.append(RoundDouble(amount));
778784
params.append(minconf);
779785
params.append(comment);
780786
params.append(comment_to);
@@ -791,7 +797,7 @@ string BitcoinAPI::sendmany(const string& fromaccount, const map<string,double>&
791797

792798
Value obj(Json::objectValue);
793799
for(map<string,double>::const_iterator it = amounts.begin(); it != amounts.end(); it++){
794-
obj[(*it).first] = (*it).second;
800+
obj[(*it).first] = RoundDouble((*it).second);
795801
}
796802

797803
params.append(obj);
@@ -808,7 +814,7 @@ string BitcoinAPI::sendmany(const string& fromaccount, const map<string,double>&
808814

809815
Value obj(Json::objectValue);
810816
for(map<string,double>::const_iterator it = amounts.begin(); it != amounts.end(); it++){
811-
obj[(*it).first] = (*it).second;
817+
obj[(*it).first] = RoundDouble((*it).second);
812818
}
813819

814820
params.append(obj);
@@ -1156,7 +1162,7 @@ string BitcoinAPI::createrawtransaction(const vector<txout_t>& inputs, const map
11561162

11571163
Value obj(Json::objectValue);
11581164
for(map<string,double>::const_iterator it = amounts.begin(); it != amounts.end(); it++){
1159-
obj[(*it).first] = (*it).second;
1165+
obj[(*it).first] = RoundDouble((*it).second);
11601166
}
11611167

11621168
params.append(vec);

src/bitcoinapi/bitcoinapi.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class BitcoinAPI
3232
Json::Value sendcommand(const std::string& command, const Json::Value& params);
3333
std::string NumberToString(int num);
3434
int StringToNumber(const std::string& str);
35+
double RoundDouble(double num);
3536

3637

3738
/* === Node functions === */

src/bitcoinapi/exception.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include <string>
1414
#include <sstream>
15+
#include <iostream>
1516

1617
#include <jsoncpp/json/json.h>
1718
#include <jsoncpp/json/reader.h>

0 commit comments

Comments
 (0)