rust-decimal icon indicating copy to clipboard operation
rust-decimal copied to clipboard

How do i use it with parsing user inputs and data structs

Open jzoudavy opened this issue 3 years ago • 2 comments

I am trying to use it with my mortgage calculator to learn rust, but I am not sure I am doing it correctly or not.

The full code is here but I have pulled out specific parts that I have problem with. https://pastebin.com/3qbGtApe

Q1: Do I use it in structs like this?

struct Mortgage {
    f64_purchase_price: Decimal,
    f64_down_payment: Decimal,
    i32_amortization_period: i32,
    f64_interest_rate1: Decimal,
    i32_interest_rate_term1: i32,
}

Q2: and i have this args parse function to take user cmd line arguments and it was f64 before but now i want to use your decimal crate, but i am getting errors:

    let args: Vec<String> = std::env::args().collect(); // get all arguements passed to app 
      ...
    let mortgage = Mortgage::parse_args(&args).unwrap_or_else(|err| {
        println!("Problem parsing arguments: {err}");
        process::exit(1);
    });

   ...
impl Mortgage {
    fn parse_args(args: &[String]) -> Result<Mortgage, &'static str> {

        let purchase_price= &args[1];
...
    

        let f64_purchase_price = dec!(purchase_price.parse().unwrap());

But doing that gives me errors:


~/rust/mortgage_calc/src$ cargo build
error: proc macro panicked
  --> src/main.rs:52:34
   |
52 |         let f64_purchase_price = dec!(purchase_price.parse().unwrap());
   |                                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = help: message: Invalid decimal: unknown character

jzoudavy avatar Sep 19 '22 00:09 jzoudavy

Hi @jzoudavy - thanks for trying out the library!

To answer your questions:

Q1: Yes, you can use it in structs just like that. Q2: The reason for this is that you're using the decimal macro as well as the built in parse methods. The macro (i.e. dec!) is a helper function explicitly for helping you construct a Decimal without having to "parse" it at runtime. For example: dec!(1.234) will generate a Decimal with the number 1.234 but instead calculates the internal representation for the Decimal at compile time instead of at runtime.

The parse syntax is correct for what you're wanting to do, however you may need a type hint to help the compiler know that you're converting it to a Decimal. For example, the following should work:

    let data = "1.234";
    let value: Decimal = data.parse().unwrap();
    println!("{}", value);

So in summary - you don't need the dec! macro here - you should be able to just use purchase_price.parse().unwrap() provided there is a type hint in there somewhere (e.g. either explictly on the same line, or while constructing the struct).

paupino avatar Sep 19 '22 15:09 paupino

cool thanks for the quick reply it works now. does Decimal have power function? I was getting errors with some of the values in the struc as i32 and some as Decimal so I changed everything to decimal but now I am getting this error:

error[E0599]: no method named `powf` found for struct `rust_decimal::Decimal` in the current scope
  --> src/main.rs:79:41
   |
79 |     let bottom_rate_power = bottom_rate.powf(mortgage.i32_amortization_period);
   |                                         ^^^^ method not found in `rust_decimal::Decimal`

jzoudavy avatar Sep 21 '22 02:09 jzoudavy

Hi @jzoudavy,

Yes, it does - though it is currently feature gated. It currently requires the maths feature to enable these features. e.g.

rust_decimal = { version = "1.26", features = ["maths"] }

More information about the powf functions can be found here.

Side note: I may update the documentation to mention the feature gates since it isn't immediately obvious in the docs.

paupino avatar Sep 21 '22 17:09 paupino