From 7ebb356a319fd610879c09c40698c785d046e253 Mon Sep 17 00:00:00 2001 From: volzhs Date: Wed, 8 Feb 2017 10:40:35 +0900 Subject: [PATCH] Fix zero padding formatting --- core/ustring.cpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/core/ustring.cpp b/core/ustring.cpp index df38153516f..9f7c263dd8b 100644 --- a/core/ustring.cpp +++ b/core/ustring.cpp @@ -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;