@@ -133,23 +133,23 @@ String::String(unsigned long long value, unsigned char base)
133133
134134String::String (float value, unsigned char decimalPlaces)
135135{
136- int len = digitsBe4Decimal (value);
136+ int totalSize = digitsBe4Decimal (value);
137137 init ();
138138
139- if (decimalPlaces) len += 1 + ((int )decimalPlaces & 0x0FF );
139+ if (decimalPlaces) totalSize += 1 + ((int )decimalPlaces & 0x0FF );
140140
141- char buf[len +1 ];
141+ char buf[totalSize +1 ];
142142 *this = dtostrf (value, 0 , decimalPlaces, buf);
143143}
144144
145145String::String (double value, unsigned char decimalPlaces)
146146{
147- int len = digitsBe4Decimal (value);
147+ int totalSize = digitsBe4Decimal (value);
148148 init ();
149149
150- if (decimalPlaces) len += 1 + ((int )decimalPlaces & 0x0FF );
150+ if (decimalPlaces) totalSize += 1 + ((int )decimalPlaces & 0x0FF );
151151
152- char buf[len +1 ];
152+ char buf[totalSize +1 ];
153153 *this = dtostrf (value, 0 , decimalPlaces, buf);
154154}
155155
@@ -814,18 +814,17 @@ float String::toFloat(void) const
814814
815815int String::digitsBe4Decimal (double number)
816816{
817- int cnt = 0 ;
818- long tmp = (long )number; // Drop the decimal here
817+ int cnt = 1 ; // Always has one digit
819818
820819 // Count -ve sign as one digit
821- if (tmp < 0 ) {
820+ if (number < 0. 0 ) {
822821 cnt++;
823- tmp = -tmp ;
822+ number = -number ;
824823 }
825824
826- // Count the number of digit
827- while (tmp ) {
828- tmp /= 10 ;
825+ // Count the number of digits beyond the 1st, basically, the exponent.
826+ while (number >= 10.0 ) {
827+ number /= 10 ;
829828 cnt++;
830829 }
831830 return cnt;
0 commit comments