core/strconv/decimal
strconv_decimal
Types
1Procedures
13assign
assign :: proc(a: ^Decimal, idx: u64)SourceConverts a given u64 integer idx to its Decimal representation in the provided Decimal struct.
Used for internal Decimal Operations.
Inputs
- a: Where the result will be stored
- idx: The value to be assigned to the Decimal
can_round_up
can_round_up :: proc(a: ^Decimal, nd: int) -> (bool)SourceDetermines if the Decimal can be rounded up at the given digit index
Inputs
- a: The Decimal to check
- nd: The digit index to consider for rounding up
Returns Boolean if can be rounded up at the given index (>=5)
decimal_to_string
decimal_to_string :: proc(buf: []u8, a: ^Decimal) -> (string)SourceConverts a Decimal to a string representation, using the provided buffer as storage.
Inputs
- buf: A byte slice buffer to hold the resulting string
- a: The struct to be converted to a string
Returns
- A string representation of the Decimal
println
println :: proc(args)Sourceround
round :: proc(a: ^Decimal, nd: int)SourceRounds the Decimal at the given digit index
Inputs
- a: The Decimal to be modified
- nd: The digit index to round
round_down
round_down :: proc(a: ^Decimal, nd: int)SourceRounds down the decimal value to the specified number of decimal places
Inputs
- a: The Decimal value to be rounded down
- nd: The number of decimal places to round down to
Example:
import "core:fmt"
import "core:strconv/decimal"
round_down_example :: proc() {
d: decimal.Decimal
str := [64]u8{}
ok := decimal.set(&d, "123.456")
decimal.round_down(&d, 5)
fmt.println(decimal.decimal_to_string(str[:], &d))
}Output:
123.45round_up
round_up :: proc(a: ^Decimal, nd: int)SourceRounds the Decimal up at the given digit index
Inputs
- a: The Decimal to be modified
- nd: The digit index to round up
rounded_integer
rounded_integer :: proc(a: ^Decimal) -> (u64)SourceExtracts the rounded integer part of a decimal value
Inputs
- a: A pointer to the Decimal value to extract the rounded integer part from
WARNING: There are no guarantees about overflow.
Returns The rounded integer part of the input decimal value
Example:
import "core:fmt"
import "core:strconv/decimal"
rounded_integer_example :: proc() {
d: decimal.Decimal
ok := decimal.set(&d, "123.456")
fmt.println(decimal.rounded_integer(&d))
}Output:
123set
set :: proc(d: ^Decimal, s: string) -> (ok: bool)SourceSets a Decimal from a given string s. The string is expected to represent a float. Stores parsed number in the given Decimal structure. If parsing fails, the Decimal will be left in an undefined state.
Inputs
- d: Pointer to a Decimal struct where the parsed result will be stored
- s: The input string representing the floating-point number
Returns
- ok: A boolean indicating whether the parsing was successful
shift
shift :: proc(a: ^Decimal, i: int)SourceShifts the decimal of the input value by the specified number of places
Inputs
- a: The Decimal to be modified
- i: The number of places to shift the decimal (positive for left shift, negative for right shift)
shift_left
shift_left :: proc(a: ^Decimal, k: uint)SourceShifts the decimal of the input value to the left by k places
WARNING: asserts k < 61
Inputs
- a: The Decimal to be modified
- k: The number of places to shift the decimal to the left
shift_right
shift_right :: proc(a: ^Decimal, k: uint)SourceShifts the Decimal value to the right by k positions.
Used for internal Decimal Operations.
Inputs
- a: The Decimal struct to be shifted
- k: The number of positions to shift right
trim
trim :: proc(a: ^Decimal)SourceTrims trailing zeros in the given Decimal, updating the count and decimal_point values as needed.
Inputs
- a: Pointer to the Decimal struct to be trimmed