mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
add primitive ftoa, dtoa to strings library.
R=rsc DELTA=72 (69 added, 0 deleted, 3 changed) OCL=17478 CL=17480
This commit is contained in:
parent
a11d5aecb8
commit
03a9872fe3
2 changed files with 72 additions and 3 deletions
|
|
@ -168,7 +168,7 @@ export func atoi(s string) (i int, ok bool) {
|
||||||
return i, okok
|
return i, okok
|
||||||
}
|
}
|
||||||
|
|
||||||
export func itol(i int64) string {
|
export func ltoa(i int64) string {
|
||||||
if i == 0 {
|
if i == 0 {
|
||||||
return "0"
|
return "0"
|
||||||
}
|
}
|
||||||
|
|
@ -197,5 +197,70 @@ export func itol(i int64) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
export func itoa(i int) string {
|
export func itoa(i int) string {
|
||||||
return itol(int64(i));
|
return ltoa(int64(i));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert float64 to string. No control over format.
|
||||||
|
// Result not great; only useful for simple debugging.
|
||||||
|
export func dtoa(v float64) string {
|
||||||
|
var buf [20]byte;
|
||||||
|
|
||||||
|
const n = 7; // digits printed
|
||||||
|
e := 0; // exp
|
||||||
|
var sign byte = '+';
|
||||||
|
if(v != 0) {
|
||||||
|
// sign
|
||||||
|
if(v < 0) {
|
||||||
|
v = -v;
|
||||||
|
sign = '-';
|
||||||
|
}
|
||||||
|
|
||||||
|
// normalize
|
||||||
|
for v >= 10 {
|
||||||
|
e++;
|
||||||
|
v /= 10;
|
||||||
|
}
|
||||||
|
for v < 1 {
|
||||||
|
e--;
|
||||||
|
v *= 10;
|
||||||
|
}
|
||||||
|
|
||||||
|
// round
|
||||||
|
var h float64 = 5;
|
||||||
|
for i := 0; i < n; i++ {
|
||||||
|
h /= 10;
|
||||||
|
}
|
||||||
|
v += h;
|
||||||
|
if v >= 10 {
|
||||||
|
e++;
|
||||||
|
v /= 10;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// format +d.dddd+edd
|
||||||
|
buf[0] = sign;
|
||||||
|
for i := 0; i < n; i++ {
|
||||||
|
s := int64(v);
|
||||||
|
buf[i+2] = byte(s)+'0';
|
||||||
|
v -= float64(s);
|
||||||
|
v *= 10;
|
||||||
|
}
|
||||||
|
buf[1] = buf[2];
|
||||||
|
buf[2] = '.';
|
||||||
|
|
||||||
|
buf[n+2] = 'e';
|
||||||
|
buf[n+3] = '+';
|
||||||
|
if e < 0 {
|
||||||
|
e = -e;
|
||||||
|
buf[n+3] = '-';
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: exponents > 99?
|
||||||
|
buf[n+4] = byte((e/10) + '0');
|
||||||
|
buf[n+5] = byte((e%10) + '0');
|
||||||
|
return string(buf)[0:n+6]; // TODO: should be able to slice buf
|
||||||
|
}
|
||||||
|
|
||||||
|
export func ftoa(v float) string {
|
||||||
|
return dtoa(float64(v));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -98,11 +98,15 @@ func main() {
|
||||||
n, ok = strings.atoi("20ba"); if n != 0 || ok { panic("atoi 20ba") }
|
n, ok = strings.atoi("20ba"); if n != 0 || ok { panic("atoi 20ba") }
|
||||||
n, ok = strings.atoi("hello"); if n != 0 || ok { panic("hello") }
|
n, ok = strings.atoi("hello"); if n != 0 || ok { panic("hello") }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if strings.ftoa(1e6) != "+1.000000e+06" { panic("ftoa 1e6") }
|
||||||
|
if strings.ftoa(-1e-6) != "-1.000000e-06" { panic("ftoa -1e-6") }
|
||||||
|
if strings.ftoa(-1.234567e-6) != "-1.234567e-06" { panic("ftoa -1.234567e-6") }
|
||||||
|
|
||||||
if itoa(0) != "0" { panic("itoa 0") }
|
if itoa(0) != "0" { panic("itoa 0") }
|
||||||
if itoa(12345) != "12345" { panic("itoa 12345") }
|
if itoa(12345) != "12345" { panic("itoa 12345") }
|
||||||
if itoa(-1<<31) != "-2147483648" { panic("itoa 1<<31") }
|
if itoa(-1<<31) != "-2147483648" { panic("itoa 1<<31") }
|
||||||
|
|
||||||
// should work if int == int64: is there some way to know?
|
// should work if int == int64: is there some way to know?
|
||||||
// if itoa(-1<<63) != "-9223372036854775808" { panic("itoa 1<<63") }
|
// if itoa(-1<<63) != "-9223372036854775808" { panic("itoa 1<<63") }
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue