Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
go
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
go
Commits
03a9872f
Commit
03a9872f
authored
Oct 20, 2008
by
Rob Pike
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add primitive ftoa, dtoa to strings library.
R=rsc DELTA=72 (69 added, 0 deleted, 3 changed) OCL=17478 CL=17480
parent
a11d5aec
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
72 additions
and
3 deletions
+72
-3
src/lib/strings.go
src/lib/strings.go
+67
-2
test/stringslib.go
test/stringslib.go
+5
-1
No files found.
src/lib/strings.go
View file @
03a9872f
...
...
@@ -168,7 +168,7 @@ export func atoi(s string) (i int, ok bool) {
return
i
,
okok
}
export
func
itol
(
i
int64
)
string
{
export
func
ltoa
(
i
int64
)
string
{
if
i
==
0
{
return
"0"
}
...
...
@@ -197,5 +197,70 @@ export func itol(i int64) 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
));
}
test/stringslib.go
View file @
03a9872f
...
...
@@ -98,11 +98,15 @@ func main() {
n
,
ok
=
strings
.
atoi
(
"20ba"
);
if
n
!=
0
||
ok
{
panic
(
"atoi 20ba"
)
}
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
(
12345
)
!=
"12345"
{
panic
(
"itoa 12345"
)
}
if
itoa
(
-
1
<<
31
)
!=
"-2147483648"
{
panic
(
"itoa 1<<31"
)
}
// should work if int == int64: is there some way to know?
// if itoa(-1<<63) != "-9223372036854775808" { panic("itoa 1<<63") }
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment