Comments (10)
Thanks @qznc.
from d-money.
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.
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.
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.
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.
@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.
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:
from d-money.
Thanks @mingwugmail - I taken notes for later.
from d-money.
FYI
from d-money.
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)
- import and template HOT 3
- 'currency' is not defined HOT 2
- assert(EUR(100.0001) == EUR(100.00009)); // wtf?! HOT 2
- string to d-money HOT 8
- EUR(0.001234) becomes EUR(0.1234)??? HOT 1
- No format specifier to convert to string without currency name
- currency.this(string) isn't constructs negative values HOT 4
- we should be able to *divide* money amounts of the same type, e.g. Bill Gates is 123456.789 x richer than me.
- we need add opCast, e.g. back to double HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from d-money.