Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Guidance for a Rust newbie - Decimal constant? #400

Closed
nwillc-paxos opened this issue Jul 2, 2021 · 3 comments · Fixed by #407
Closed

Guidance for a Rust newbie - Decimal constant? #400

nwillc-paxos opened this issue Jul 2, 2021 · 3 comments · Fixed by #407

Comments

@nwillc-paxos
Copy link

Given:

fn percent_change(prior: Decimal, current: Decimal) -> Decimal {
    (current - prior) / prior * Decimal::from(100)
}

Is there a way to have a Decimal::ONE_HUNDRED constant?

@schungx
Copy link
Contributor

schungx commented Jul 3, 2021

I would second this, as it is usually quite convenient to have the following constants, in addition to ZERO, ONE, TWO and PI:

NEGATIVE_ONE (useful as a step)
TEN
HUNDRED (useful for percentage calculations), can be simulated via TEN * TEN
THOUSAND (useful for K calculations), can be simulated via TEN * TEN * TEN
HALF_PI (useful as a 90-deg angle), can be simulated via PI / TWO
TWO_PI (useful as a full circle), can be simulated via PI * TWO
E (useful for scientific)

@c410-f3r
Copy link
Contributor

c410-f3r commented Jul 3, 2021

It is currently possible to create a constant Decimal through the from_parts method.

const DECIMAL_0_001: Decimal = Decimal::from_parts(1, 0, 0, false, 3);

Once rust-lang/rust#51999 is stabilized, it will be possible to write

const PI: Decimal = Decimal::try_from_i128_with_scale(3141_i128, 3).unwrap();

@schungx
Copy link
Contributor

schungx commented Jul 4, 2021

Yes, but in actual usage, it is simpler to just do Dynamic::TEN without having to remember to first create the TEN const somewhere...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants