File tree Expand file tree Collapse file tree 2 files changed +41
-5
lines changed Expand file tree Collapse file tree 2 files changed +41
-5
lines changed Original file line number Diff line number Diff line change @@ -890,10 +890,46 @@ float String::toFloat(void) const
890
890
return float (toDouble ());
891
891
}
892
892
893
- double String::toDouble (void ) const
894
- {
895
- if (buffer) {
896
- return atof (buffer);
893
+ float String::toDouble (void ) const
894
+ {
895
+ if (buffer)
896
+ {
897
+ char *p = buffer;
898
+ float f = 0.0 ;
899
+ int i = -1 ;
900
+ // 分割一下字符串
901
+ if (p[0 ] != ' \0 ' ) // 小数点不在开头
902
+ {
903
+ i = 0 ;
904
+ while (p[i] != ' \0 ' )
905
+ {
906
+ if (p[i] == ' .' )
907
+ {
908
+ p[i] = ' \0 ' ;
909
+ i++;
910
+ break ;
911
+ }
912
+ i++;
913
+ }
914
+ f += atol (p);
915
+ }
916
+ else // 开头是小数点
917
+ {
918
+ i = 1 ;
919
+ }
920
+ if (i != -1 ) // 有小数点
921
+ {
922
+ int p_sum = strlen (p + i);
923
+ float f2 = atol (p + i);
924
+ while (p_sum--)
925
+ f2 /= 10.0 ;
926
+ f += f2;
927
+ }
928
+ else // 没有小数点
929
+ {
930
+ f = atol (p);
931
+ }
932
+ return f;
897
933
}
898
934
return 0 ;
899
935
}
Original file line number Diff line number Diff line change @@ -271,7 +271,7 @@ class String {
271
271
// parsing/conversion
272
272
long toInt (void ) const ;
273
273
float toFloat (void ) const ;
274
- double toDouble (void ) const ;
274
+ float toDouble (void ) const ;
275
275
276
276
protected:
277
277
char *buffer; // the actual char array
You can’t perform that action at this time.
0 commit comments