package core:strconv/decimal
Overview
Multiple precision decimal numbers NOTE: This is only for floating point printing and nothing else
Index
Types (1)
Constants (0)
This section is empty.
Variables (0)
This section is empty.
Procedure Groups (0)
This section is empty.
Types
Decimal ¶
Decimal :: struct { digits: [384]u8, // big-endian digits count: int, decimal_point: int, neg: bool, trunc: bool, }
Related Procedures With Parameters
Constants
This section is empty.
Variables
This section is empty.
Procedures
assign ¶
Converts 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 ¶
Determines 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 ¶
Converts 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
round ¶
Rounds the Decimal at the given digit index
Inputs
a: The Decimal to be modified
nd: The digit index to round
round_down ¶
Rounds 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))
}
123.45
round_up ¶
Rounds the Decimal up at the given digit index
Inputs
a: The Decimal to be modified
nd: The digit index to round up
rounded_integer ¶
Extracts 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))
}
123
set ¶
Sets 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 ¶
Shifts 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 ¶
Shifts 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 ¶
Shifts 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) {…}
Trims 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
Procedure Groups
This section is empty.
Source Files
Generation Information
Generated with odin version dev-2024-11 (vendor "odin") Windows_amd64 @ 2024-11-16 21:10:10.084046400 +0000 UTC