How to represent currency in Rust

1 Answer

0 votes
/*---------------------------------------------------------
  Description:
    Demonstrates how to represent and display currency
    values in Rust using formatted output.
---------------------------------------------------------*/

fn main() {
    // Price and tax rate
    let price: f64 = 199.99;
    let tax_rate: f64 = 0.17; // 17%

    // Calculate tax and total
    let tax_amount = price * tax_rate;
    let total = price + tax_amount;

    // Output with $ and % signs
    println!("Price: ${:.2}", price);
    println!("Tax Rate: 17%");
    println!("Tax Amount: ${:.2}", tax_amount);
    println!("Total: ${:.2}", total);
}


/*
run:

Price: $199.99
Tax Rate: 17%
Tax Amount: $34.00
Total: $233.99

*/

 



answered 12 hours ago by avibootz
...