-
Notifications
You must be signed in to change notification settings - Fork 37.7k
Closed
Description
The problem is in the FormatMoney function, in util.cpp. This comment includes a fixed version, which requires
two support functions, which are also included:
int64
last_digit(int64 n) {
int64 m = n / (int64)10;
m *= (int64)10;
return n - m;
}
char
todigit(int64 n) {
static char digits[] = "0123456789";
return digits[n];
}
string
FormatMoney(int64 n, bool fPlus)
{
string str;
bool was_negative = n<0;
bool omit_digit = true; // Omit trailing zeros.
size_t ix=0;
if(n == 0) {
str.insert(0,1,'0');
return str;
}
if(n < 0) {
n = -n;
}
// Process the number from right to left.
for(ix=0; n || ix<8; ix++) {
int64 next = last_digit(n);
n /= (int64)10;
if(next || ix>=8) omit_digit=false;
if(!omit_digit) {
str.insert(0,1,todigit(next));
if(ix == 7) str.insert(0,1,'.');
}
}
if(was_negative) str.insert(0,1,'-');
else if(fPlus) str.insert(0,1,'+');
return str;
}
Metadata
Metadata
Assignees
Labels
No labels