Fix zero padding formatting

This commit is contained in:
volzhs 2017-02-08 10:40:35 +09:00
parent 34b6caa433
commit 7ebb356a31
1 changed files with 9 additions and 6 deletions

View File

@ -3945,12 +3945,8 @@ String String::sprintf(const Array& values, bool* error) const {
case 'X': base = 16; capitalize = true; break;
}
// Get basic number.
String str = String::num_int64(value, base, capitalize);
// Sign.
if (show_sign && value >= 0) {
str = str.insert(0, "+");
}
String str = String::num_int64(ABS(value), base, capitalize);
int number_len = str.length();
// Padding.
String pad_char = pad_with_zeroes ? String("0") : String(" ");
@ -3960,6 +3956,13 @@ String String::sprintf(const Array& values, bool* error) const {
str = str.lpad(min_chars, pad_char);
}
// Sign.
if (show_sign && value >= 0) {
str = str.insert(pad_with_zeroes?0:str.length()-number_len, "+");
} else if (value < 0) {
str = str.insert(pad_with_zeroes?0:str.length()-number_len, "-");
}
formatted += str;
++value_index;
in_format = false;