Coder Social home page Coder Social logo

Comments (4)

paupino avatar paupino commented on May 26, 2024 1

Thanks for this submission. It is something that I've considered adding, in addition to supporting different radix. I'll have a play and see what I can come up with over the next week. I suspect we can get some nice optimizations here since we essentially store in a scientific format already (i.e. value * 10^scale).

from rust-decimal.

andoriyu avatar andoriyu commented on May 26, 2024

Something like that:

    fn from_scientific(value: &str) -> Result<Decimal, Error> {
        let err = Error::new("Failed to parse");
        let mut split = value.splitn(2, 'e');

        let base = split.next().ok_or(err.clone())?;
        let mut scale = split.next().ok_or(err.clone())?.to_string();

        let mut ret = Decimal::from_str(base)?;

        if scale.contains('-') {
            scale.remove(0);
           let scale: u32 = scale.as_str().parse().map_err(move |_| err.clone())?;
           let current_scale = ret.scale();
           ret.set_scale(current_scale+ scale)?;
        } else {
            if scale.contains('+') {
                scale.remove(0);
            }
            let pow: u32 = scale.as_str().parse().map_err(move |_| err.clone())?;
            ret *= Decimal::from_i64(10_i64.pow(pow)).unwrap();
        }
        Ok(ret)
    }

from rust-decimal.

paupino avatar paupino commented on May 26, 2024

Sorry for the delay in getting this one through. I've got a working example on the branch feature/scientific however want to see if I can improve the performance further. That being said, since it is likely a low use call I could probably merge it and improve it later on.

from rust-decimal.

andoriyu avatar andoriyu commented on May 26, 2024

@paupino yeah, even mess I wrote was working fine for my use case.

from rust-decimal.

Related Issues (20)

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.