MCPcopy Create free account
hub / github.com/cockroachdb/apd / ExampleContext_RoundToIntegralExact

Function ExampleContext_RoundToIntegralExact

example_test.go:112–139  ·  view source on GitHub ↗

ExampleRoundToIntegralExact demonstrates how to use RoundToIntegralExact to check if a number is an integer or not. Note the variations between integer (which allows zeros after the decimal point) and strict (which does not). See the documentation on Inexact and Rounded.

()

Source from the content-addressed store, hash-verified

110// (which allows zeros after the decimal point) and strict (which does not). See
111// the documentation on Inexact and Rounded.
112func ExampleContext_RoundToIntegralExact() {
113 inputs := []string{
114 "123.4",
115 "123.0",
116 "123",
117 "12E1",
118 "120E-1",
119 "120E-2",
120 }
121 for _, input := range inputs {
122 d, _, _ := apd.NewFromString(input)
123 res, _ := apd.BaseContext.RoundToIntegralExact(d, d)
124 integer := !res.Inexact()
125 strict := !res.Rounded()
126 fmt.Printf("input: % 6s, output: %3s, integer: %5t, strict: %5t, res:", input, d, integer, strict)
127 if res != 0 {
128 fmt.Printf(" %s", res)
129 }
130 fmt.Println()
131 }
132 // Output:
133 // input: 123.4, output: 123, integer: false, strict: false, res: inexact, rounded
134 // input: 123.0, output: 123, integer: true, strict: false, res: rounded
135 // input: 123, output: 123, integer: true, strict: true, res:
136 // input: 12E1, output: 120, integer: true, strict: true, res:
137 // input: 120E-1, output: 12, integer: true, strict: false, res: rounded
138 // input: 120E-2, output: 1, integer: false, strict: false, res: inexact, rounded
139}

Callers

nothing calls this directly

Calls 4

NewFromStringMethod · 0.80
InexactMethod · 0.80
RoundedMethod · 0.80
RoundToIntegralExactMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…