Apa v0.3.0 ApaNumber View Source

APA : Arbitrary Precision Arithmetic - Number String helper - ApaNumber.

Helper to handle number string inputs convert any number string to a tuple of 2 integers: {integer_value, exp}

Link to this section Summary

Functions

clean number strings - signs, leading and trailing zeros etc.

Parses a binary (number string) into an ApaNumber tuple.

Shift an ApaNumber to another decimal point to work with the intended integer calculation of two numbers with the same decimal point this operation do not change the mathematical value: ApaNumber.to_string({2, -1}) == "0.2" ~math-equal~ ApaNumber.to_string({20, -2}) == "0.20" and always shift_decimal_point > exp because fillup with zeros reduce non existing zeros is not possible and not necessary to implement

Create a string from an ApaNumber tuple.

Link to this section Functions

Link to this function

add_minus_sign(number_string)

View Source

clean number strings - signs, leading and trailing zeros etc.

Examples

iex> ApaNumber.clean("0003")
"3"

iex> ApaNumber.clean("+0003")
"3"

iex> ApaNumber.clean("-0003")
"3"

iex> ApaNumber.clean("0000120.1200")
"120.12"
Link to this function

clean_abs_float_string(float_string)

View Source
Link to this function

clean_abs_int_string(int_string)

View Source
Link to this function

clean_float_string(float_string)

View Source
Link to this function

diff_count(calc_string, diff_string)

View Source
Link to this function

digits_list_reverse(digits_string)

View Source
Link to this function

fill_if_empty(int_string)

View Source
Link to this function

fill_if_needed(int_string, abs_decimal_point)

View Source
Link to this function

fill_up_string_leading_zeros(fill_up_string, zeros_count)

View Source
Link to this function

fill_up_string_trailing_zeros(fill_up_string, zeros_count)

View Source
Link to this function

fill_up_trailing_zeros(int_value, count_zeros)

View Source
Link to this function

from_string(binary)

View Source
from_string(binary()) :: {integer(), integer()} | :error

Parses a binary (number string) into an ApaNumber tuple.

It works with signs, leading and trailing zeros and additional chars will be ignored. If successful, returns a tuple in the form of {integer_value, exponent}:

ApaNumber.from_string("+0003.00e+00000 Dollar") {3, 0}

When the binary cannot be parsed, the atom :error will be returned.

The limit only depends on the internal integers - because of Elixir "unlimited" integers I would say "arbitrary".

Used elixir source from Float module for parsing - nice source of inspiration! Thank you José!

Examples

iex> ApaNumber.from_string("0003")
{3, 0}

iex> ApaNumber.from_string("+0003")
{3, 0}

iex> ApaNumber.from_string("-0003")
{-3, 0}

iex> ApaNumber.from_string("-0000120.1200")
{-12012, -2}

iex> ApaNumber.from_string("-0000120.1200")
{-12012, -2}

iex> ApaNumber.from_string("-03 Euro")
{-3, 0}

iex> ApaNumber.from_string("-0003e-2")
{-3, -2}

iex> ApaNumber.from_string("-3e-0002")
{-3, -2}

iex> ApaNumber.from_string("3e-12")
{3, -12}

iex> ApaNumber.from_string("+0003e+12")
{3000000000000, 0}

iex> ApaNumber.from_string("+0003e+00000")
{3, 0}

iex> ApaNumber.from_string("+0003.00e+00000 Dollar")
{3, 0}
Link to this function

remove_leading_zeros(list)

View Source
Link to this function

shift_to(arg, shift_decimal_point)

View Source

Shift an ApaNumber to another decimal point to work with the intended integer calculation of two numbers with the same decimal point this operation do not change the mathematical value: ApaNumber.to_string({2, -1}) == "0.2" ~math-equal~ ApaNumber.to_string({20, -2}) == "0.20" and always shift_decimal_point > exp because fillup with zeros reduce non existing zeros is not possible and not necessary to implement

Examples

iex> ApaNumber.shift_to({2, -1}, -2)
{20, -2}

iex> ApaNumber.shift_to({2, -1}, -4)
{2000, -4}

Create a string from an ApaNumber tuple.

Examples

iex> ApaNumber.to_string({3, 0})
"3"

iex> ApaNumber.to_string({-3, 0})
"-3"

iex> ApaNumber.to_string({3, 3})
"3000"

iex> ApaNumber.to_string({-12012, -2})
"-120.12"

iex> ApaNumber.to_string({-3997, -6})
"-0.003997"