Git Product home page Git Product logo

decimal-percentage-rs's Introduction

Percentage Type with Decimal

Build Status MIT Licensed Crates.io decimal-percentage

A type to represent percentage with high precision thanks to rust_decimal.

A percentage can perform addition, subtraction and multiplication.

use decimal_percentage::Percentage;
use rust_decimal::Decimal;

let p1 = Percentage::from(0.1f64);
let p2 = Percentage::from(0.1f32);
let p3 = Percentage::try_from("0.1").unwrap();
let p4 = Percentage::from(Decimal::from_f64(0.3).unwrap());

assert_eq!(p1 + p2, Percentage::from(0.2));
assert_eq!(p1 + 0.2, Percentage::from(0.3));
assert_eq!(p4 - p2, Percentage::from(0.2));
assert_eq!(p1 * 66.0, 6.6);
assert_eq!(p1 * 100u32, 10u32);
assert_eq!(p1 * -100i32, -10i32);
// note that a multiplication to integer type can lose precision
assert_eq!(p1 * -33i32, -3i32);
// multiplication on extremely small value with Decimal,
// that is not representable with float point
let small_value = Decimal::from_str("0.0000000000000000002").unwrap();
assert_eq!(p1 * small_value, Decimal::from_str("0.00000000000000000002").unwrap());

Contributing

Contributions and feed back are welcome following Github workflow.

License

decimal_percentage is provided under the MIT license. See LICENSE.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.