Currency: Difference between revisions

Content added Content deleted
(Added Rust solution)
Line 1,276: Line 1,276:


fn main() {
fn main() {
let hamburger = Currency::new(550,100); //5,5
let hamburger = Currency::new(5.50);
let milkshake = Currency::new(286,100); //2,86
let milkshake = Currency::new(2.86);
let pre_tax = hamburger * 4_000_000_000_000_000 + milkshake * 2;
let pre_tax = hamburger * 4_000_000_000_000_000 + milkshake * 2;
println!("Price before tax: {}", pre_tax);
println!("Price before tax: {}", pre_tax);
Line 1,322: Line 1,322:
impl Currency {
impl Currency {
fn new(num: i64, den: i64) -> Self {
fn new(num: f64) -> Self {
Self {
Self {
amount: BigRational::new(num.into(), den.into())
amount: BigRational::new(((num * 100.0) as i64).into(), 100.into())
}
}
}
}


fn calculate_tax(&self) -> Self {
fn calculate_tax(&self) -> Self {
let tax_val = BigRational::new(765.into(), 100.into());// 7.65 -> 0.0765 after the next line
let tax_val = BigRational::new(765.into(), 100.into());// 7,65 -> 0.0765 after the next line
let amount = (&self.amount * tax_val).ceil() / BigInt::from(100);
let amount = (&self.amount * tax_val).ceil() / BigInt::from(100);
Self {
Self {