Skip to content

Commit dc28ef6

Browse files
feat: expose DecimalValueFromString
to construct decimals from raw-string representation.
1 parent 92f54cf commit dc28ef6

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

examples/decimal/main.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ func main() {
9393

9494
x := big.NewInt(42 * 1000000000)
9595
x.Mul(x, big.NewInt(2))
96+
parsedDecimal, err := types.DecimalValueFromString("42.00", 22, 9)
97+
if err != nil {
98+
panic(err)
99+
}
96100

97101
_, _, err = s.Execute(ctx, txc, render(writeQuery, templateConfig{
98102
TablePathPrefix: prefix,
@@ -103,6 +107,10 @@ func main() {
103107
types.StructFieldValue("id", types.Uint32Value(42)),
104108
types.StructFieldValue("value", types.DecimalValueFromBigInt(x, 22, 9)),
105109
),
110+
types.StructValue(
111+
types.StructFieldValue("id", types.Uint32Value(43)),
112+
types.StructFieldValue("value", parsedDecimal),
113+
),
106114
),
107115
),
108116
))

table/types/value.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,14 @@ func DecimalValueFromBigInt(v *big.Int, precision, scale uint32) Value {
189189
return value.DecimalValueFromBigInt(v, precision, scale)
190190
}
191191

192+
func DecimalValueFromString(str string, precision, scale uint32) (Value, error) {
193+
bigI, err := decimal.Parse(str, precision, scale)
194+
if err != nil {
195+
return nil, err
196+
}
197+
return value.DecimalValueFromBigInt(bigI, precision, scale), nil
198+
}
199+
192200
func TupleValue(vs ...Value) Value {
193201
return value.TupleValue(vs...)
194202
}

0 commit comments

Comments
 (0)