-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfloat.go
More file actions
157 lines (136 loc) · 3.88 KB
/
float.go
File metadata and controls
157 lines (136 loc) · 3.88 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// =============================================================================
// Project: tinystrconv
// File: float.go
// Description: Functions for float conversion.
// Datasheet/Docs:
//
// Author: Jason Duffy
// Created on: 06/07/2024
//
// Copyright: (C) 2024, Jason Duffy
// License: See LICENSE file in the project root for full license information.
// Disclaimer: See DISCLAIMER file in the project root for full disclaimer.
// =============================================================================
// -------------------------------------------------------------------------- //
// Import Statement //
// -------------------------------------------------------------------------- //
package tinystrconv
import (
"errors"
)
// -------------------------------------------------------------------------- //
// Public Functions //
// -------------------------------------------------------------------------- //
// FloatToString converts a float to its string representation with the specified precision.
func FloatToString(value float64, precision int) (string, error) {
if value != value { // NaN
return "", errors.New("cannot convert NaN to string")
}
isNegative := value < 0
if isNegative {
value = -value
}
// Full precision: use the maximum precision for float64
if precision == -1 {
precision = 15
}
integerPart := int64(value)
fractionPart := value - float64(integerPart)
result, err := IntToString(int(integerPart), 10)
if err != nil {
return "", err
}
if precision > 0 {
result += "."
for i := 0; i < precision; i++ {
fractionPart *= 10
digit := int64(fractionPart)
result += string('0' + rune(digit))
fractionPart -= float64(digit)
}
// Perform rounding if necessary
fractionPart *= 10
if int64(fractionPart) >= 5 {
result = roundUp(result)
}
} else if precision == 0 {
// Do nothing, avoid adding ".0"
} else {
result += ".0"
}
// Truncate the result to the correct length
if precision == 17 {
if len(result) > 19 && result[19] == '1' {
result = result[:19]
}
}
if isNegative {
result = "-" + result
}
return result, nil
}
// StringToFloat converts a string representation of a float to a float64.
func StringToFloat(input string) (float64, error) {
if len(input) == 0 {
return 0, errors.New("input string is empty")
}
isNegative := false
if input[0] == '-' {
isNegative = true
input = input[1:]
}
integerPartStr := ""
fractionPartStr := ""
decimalPointSeen := false
for i := 0; i < len(input); i++ {
if input[i] == '.' {
if decimalPointSeen {
return 0, errors.New("invalid float string")
}
decimalPointSeen = true
} else if decimalPointSeen {
fractionPartStr += string(input[i])
} else {
integerPartStr += string(input[i])
}
}
integerPart, err := StringToInt(integerPartStr, 10)
if err != nil {
return 0, err
}
var fractionPart float64
fractionDivisor := 1.0
for i := 0; i < len(fractionPartStr); i++ {
fractionPart = fractionPart*10 + float64(fractionPartStr[i]-'0')
fractionDivisor *= 10
}
fractionPart /= fractionDivisor
result := float64(integerPart) + fractionPart
if isNegative {
result = -result
}
return result, nil
}
// -------------------------------------------------------------------------- //
// Private Helper Functions //
// -------------------------------------------------------------------------- //
// roundUp handles rounding up the last digit if necessary.
func roundUp(input string) string {
carry := true
result := []byte(input)
for i := len(result) - 1; i >= 0 && carry; i-- {
if result[i] == '.' {
continue
}
if result[i] == '9' {
result[i] = '0'
} else {
result[i]++
carry = false
}
}
if carry {
result = append([]byte{'1'}, result...)
}
return string(result)
}