-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumber.go
More file actions
72 lines (65 loc) · 1.67 KB
/
Copy pathnumber.go
File metadata and controls
72 lines (65 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// SPDX-License-Identifier: MIT
// Derived from Rough.js 4.6.6, Copyright (c) 2019 Preet Shihn.
package rough
import (
"math"
"strconv"
"strings"
)
func jsToFixedNumber(v float64, digits int) float64 {
if digits < 0 || math.IsNaN(v) || math.IsInf(v, 0) {
return v
}
formatted := strconv.FormatFloat(v, 'f', digits, 64)
result, err := strconv.ParseFloat(formatted, 64)
if err != nil {
return v
}
return result
}
// jsNumberString implements the formatting used when JavaScript coerces a
// Number to a string. Rough.js builds SVG path data with that coercion, so the
// fixed/exponential thresholds and negative-zero handling are observable.
func jsNumberString(v float64) string {
switch {
case math.IsNaN(v):
return "NaN"
case math.IsInf(v, 1):
return "Infinity"
case math.IsInf(v, -1):
return "-Infinity"
case v == 0:
// String(-0) is "0" in JavaScript.
return "0"
}
abs := math.Abs(v)
format := byte('f')
if abs < 1e-6 || abs >= 1e21 {
format = 'e'
}
s := strconv.FormatFloat(v, format, -1, 64)
if format != 'e' {
return s
}
// strconv pads single-digit negative exponents (e-07), while JavaScript
// emits e-7. Positive exponents already include the required plus sign.
if i := strings.LastIndexByte(s, 'e'); i >= 0 && i+2 < len(s) {
sign := s[i+1]
if sign == '+' || sign == '-' {
j := i + 2
for j+1 < len(s) && s[j] == '0' {
j++
}
if j != i+2 {
s = s[:i+2] + s[j:]
}
}
}
return s
}
// NumberString formats v the way JavaScript coerces a Number to a string.
// It is useful when surrounding SVG serialization must remain byte-compatible
// with Rough.js output.
func NumberString(v float64) string {
return jsNumberString(v)
}