|
| 1 | +extern crate bigdecimal; |
| 2 | +use bigdecimal::*; |
| 3 | +use std::str::FromStr; |
| 4 | + |
| 5 | +/* Default example output: |
| 6 | +
|
| 7 | +Hello, Big Decimals! |
| 8 | +Input (0.8) with 10 decimals: 0.8 vs 0.8) |
| 9 | +square 0.64 |
| 10 | +From Prim: 3.300000000000000 |
| 11 | +match test 33.90000000000000 |
| 12 | +24.00000000000000 + 34.00000000000000 = 58.00000000000000 |
| 13 | +sum mut: 48.00000000000000 |
| 14 | +0.000000000000000 IS equal to zero |
| 15 | +24.00000000000000 / 1.000000000000000 = 24.00000000000000 |
| 16 | +24.0 / 1.000000000000000 = 24.0 |
| 17 | +24.0 / 1.5 = 16 |
| 18 | +24.000 / 1.50 = 16.0 |
| 19 | +*/ |
| 20 | + |
| 21 | +fn main() { |
| 22 | + println!("Hello, Big Decimals!"); |
| 23 | + let input = "0.8"; |
| 24 | + let dec = BigDecimal::from_str(&input).unwrap(); |
| 25 | + let float = f32::from_str(&input).unwrap(); |
| 26 | + println!("Input ({}) with 10 decimals: {} vs {})", input, dec, float); |
| 27 | + |
| 28 | + let bd_square = dec.square(); |
| 29 | + println!("square {}", bd_square); |
| 30 | + |
| 31 | + let bd_from_prim = BigDecimal::from_f64(3.3); |
| 32 | + println!("From Prim: {}", bd_from_prim.unwrap()); |
| 33 | + |
| 34 | + let bd_match: BigDecimal = match BigDecimal::from_f64(33.9) { |
| 35 | + Some(bd) => bd, |
| 36 | + None => BigDecimal::zero(), |
| 37 | + }; |
| 38 | + println!("match test {}", bd_match); |
| 39 | + |
| 40 | + let bd_add1 = &BigDecimal::from_f64(24.0).unwrap(); |
| 41 | + let bd_add2 = &BigDecimal::from_f64(34.0).unwrap(); |
| 42 | + print_sum(bd_add1, bd_add2); |
| 43 | + |
| 44 | + let mut bd_add3 = BigDecimal::from_f64(24.0).unwrap(); |
| 45 | + let bd_add4 = BigDecimal::from_f64(24.0).unwrap(); |
| 46 | + bd_add3 += bd_add4; |
| 47 | + println!("sum mut: {}", bd_add3); |
| 48 | + |
| 49 | + let bd_nez = BigDecimal::from_f64(0.0).unwrap(); |
| 50 | + if bd_nez != BigDecimal::zero() { |
| 51 | + println!("{} is not equal to zero", &bd_nez); |
| 52 | + } else { |
| 53 | + println!("{} IS equal to zero", &bd_nez); |
| 54 | + } |
| 55 | + |
| 56 | + let bd_div1 = &BigDecimal::from_f64(24.0).unwrap(); |
| 57 | + let bd_div2 = &BigDecimal::from_f64(1.0).unwrap(); |
| 58 | + print_division(bd_div1, bd_div2); |
| 59 | + |
| 60 | + let bd_div1 = &BigDecimal::from_str("24.0").unwrap(); |
| 61 | + print_division(bd_div1, bd_div2); |
| 62 | + |
| 63 | + let bd_num = &BigDecimal::from_str("24.0").unwrap(); |
| 64 | + let bd_den = &BigDecimal::from_str("1.5").unwrap(); |
| 65 | + print_division(bd_num, bd_den); |
| 66 | + |
| 67 | + let bd_num = &BigDecimal::from_str("24.000").unwrap(); |
| 68 | + let bd_den = &BigDecimal::from_str("1.50").unwrap(); |
| 69 | + print_division(bd_num, bd_den); |
| 70 | +} |
| 71 | + |
| 72 | +fn print_sum(a: &BigDecimal, b: &BigDecimal) { |
| 73 | + let sum = a + b; |
| 74 | + println!("{} + {} = {}", a, b, sum); |
| 75 | +} |
| 76 | + |
| 77 | +fn print_division(num: &BigDecimal, den: &BigDecimal) { |
| 78 | + let quotient = num / den; |
| 79 | + println!("{} / {} = {}", num, den, quotient); |
| 80 | +} |
0 commit comments