Giter VIP home page Giter VIP logo

Comments (10)

joelcnz avatar joelcnz commented on August 23, 2024 1

Thanks @qznc.

from d-money.

mw66 avatar mw66 commented on August 23, 2024

by loop, I mean:

long pow10(int x)
{
  assert(x >= 0);  // you only handle non-neg x
  int r = 1;
  for (int i = x; i--> 0; ) {
    r *= 10;
  }
  return r;
}

from d-money.

mw66 avatar mw66 commented on August 23, 2024

For the fix, it's better avoid writing own regex, just use standard library:

https://stackoverflow.com/questions/12605641/d-programming-parse-or-convert-string-to-double

  amount  = to!float(str) * pow10(dec_places);
import std.conv;
import std.stdio;    

void main() {
    float x = to!float("234.32");
    double y = to!double("234.32");

    writefln("And the float is: %f\nHey, we also got a double: %f", x, y);
}

from d-money.

mw66 avatar mw66 commented on August 23, 2024

And for practical purpose, the pow10, can be just a lookup table for small values:
e.g. upto the dec_places (default to 4), which can be statically generated:

lookup_table[] = [1, 10, 100, 1000, 10000]

from d-money.

schveiguy avatar schveiguy commented on August 23, 2024

Not able to commit to this repository, I'm not sure if @qznc is still active here. But I faced a similar problem with string parsing in a similar library, mostly because of the inaccuracies of floating point (the library used the technique suggested here -- convert to float, and then to integer).

We solved this in a new library by manipulating the string to be parseable directly to the storage integer form. See the result, maybe it can be used here as well.

from d-money.

joelcnz avatar joelcnz commented on August 23, 2024

@schveiguy I've been using this library. Could you fork it and fix it? I've got money entries dating back to the middle of 2009! If @qznc has abandoned this one.

from d-money.

mw66 avatar mw66 commented on August 23, 2024

@joelcnz

for the time being, you can use this package

https://github.com/m3m0ry/fixedpoint/

e.g.

  import fixedpoint.fixed : Fixed;
  alias EUR = Fixed!4;

but this package won't check for overflows:

m3m0ry/fixedpoint#4

from d-money.

joelcnz avatar joelcnz commented on August 23, 2024

Thanks @mingwugmail - I taken notes for later.

from d-money.

mw66 avatar mw66 commented on August 23, 2024

@joelcnz

FYI

#12

from d-money.

qznc avatar qznc commented on August 23, 2024

Sorry for neglecting you. I want to maintain this further but I'm not checking Github that often anymore. Thanks everybody for discussion here!

I reviewed the pull request and only had one small request before merging it.

from d-money.

Related Issues (10)

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.