qznc / d-money Goto Github PK
View Code? Open in Web Editor NEWHandling amounts of money safely and efficiently
License: Boost Software License 1.0
Handling amounts of money safely and efficiently
License: Boost Software License 1.0
How do I take a string and convert it into the money number?
Example:
alias EUR = currency!("EUR");
EUR cost;
cost = EUR("1.23");
$ cat m.d
/+dub.sdl:
dependency "money" version="~>2.3.1"
+/
import std.stdio;
import money;
alias EUR = currency!("EUR");
void main() {
EUR x = EUR( 1.23456 );
EUR y = EUR("1.23456");
writeln(x);
writeln(y);
assert(x == y);
}
$ dub m.d
1.2346EUR
3.3456EUR
[email protected](15): Assertion failure
----------------
??:? _d_assertp [0x52b005]
m.d:15 _Dmain [0x4dec36]
Program exited with code 1
The reason is pow10(x) doesn't handle negative x at all.
https://github.com/qznc/d-money/blob/master/source/money.d#L64
when called from
https://github.com/qznc/d-money/blob/master/source/money.d#L107
with x = -1
BTW, why use a recursive function instead of a simple loop to calc pow10?
I not sure I understand dub.
I get this with the code below:
Joels-MacBook-Pro:Money joelcnz$ dub
Performing "debug" build using dmd for x86_64.
money 1.0.0: target for configuration "library" is up to date.
jmoney ~master: building configuration "application"...
source/app.d(6,14): Error: template instance currency!"EUR" template 'currency' is not defined
dmd failed with exit code 1.
Joels-MacBook-Pro:Money joelcnz$
{
"name": "jmoney",
"description": "A minimal D application.",
"dependencies":
{
"money": "~>1.0.0"
}
}
[code]
import std.stdio;
import money;
void main() {
alias EUR = currency!("EUR");
assert(EUR(100.0001) == EUR(100.00009));
assert(EUR(3.10) + EUR(1.40) == EUR(4.50));
assert(EUR(3.10) - EUR(1.40) == EUR(1.70));
assert(EUR(10.01) * 1.1 == EUR(11.011));
writefln("%d", EUR(3.6)); // "4EUR"
writefln("%f", EUR(3.141592)); // "3.1416EUR"
writefln("%.2f", EUR(3.145)); // "3.15EUR"
}
[/code]
Hi, I've the following small test example:
void main()
{
alias EUR = currency!("EUR", 6);
import std.stdio;
writeln(EUR(0.001234));
}
But writeln() prints "0.1234EUR" instead of "0.001234EUR". I also tried writefln() but the result as the same. Am I doing something wrong?
I get this:
Joels-MacBook-Pro:Money joelcnz$ dub
Performing "debug" build using dmd for x86_64.
money 1.0.0: target for configuration "library" is up to date.
jmoney ~master: building configuration "application"...
source/app.d(6,14): Error: template instance money!"EUR" money is not a template declaration, it is a import
dmd failed with exit code 1.
Joels-MacBook-Pro:Money joelcnz$
I think the module needs a new name.
When working with a database it is required to use the algorithm from toString() but we don't need a currency name. So it is required additional format specifier to have such possibility. '%F' is proposed.
/+ dub.sdl:
name "hello_world"
dependency "money" version="*"
+/
import std.stdio;
import money;
alias T1 = currency!("T1", 2);
alias T2 = currency!("T2", 2, roundingMode.UNNECESSARY);
void main() {
auto t1 = T1(-123.45);
auto t2 = T1("-123.45");
t1.writeln; // -123.-45T1 (extra minus in toString?)
t2.writeln; // positive value, but should be negative
assert(t1 == t2); // failure
}
$ dub run union_bug.d --single
Performing "debug" build using /usr/bin/dmd for x86_64.
money 2.3.0: target for configuration "library" is up to date.
hello_world ~master: building configuration "application"...
Linking...
To force a rebuild of up-to-date targets, run again with --force.
Running ./hello_world
-123.-45T1
123.45T1
core.exception.AssertError@union_bug.d(19): Assertion failure
----------------
??:? _d_assertp [0x77087198]
union_bug.d:19 _Dmain [0x7703cfaf]
Program exited with code 1
we should be able to divide money amounts of the same type, e.g. Bill Gates is 123456.789 x richer than me.
The return type is double, not sure if D lang support this?
https://github.com/qznc/d-money/blob/master/source/money.d#L149
/// Can *divide* money amounts of the same type.
double opBinary(string op)(const T rhs) const {...
else static if (op == "/")
}
Hi!
Sorry for my stupid question, but I can't understand what means:
assert(EUR(100.0001) == EUR(100.00009));
As I understand, IRL 100.0001 Euros isn't equal 100.00009 Euros - this is different amounts. And, for example, if I will use this lib for counting of total after some bank/stock exchange transaction this behaviour can't help me to find calculation errors.
e.g. after calculating the money value using this library, the value needs to feed into another library for some math operation (or for drawing a chart, i.e money => pixel height), which takes D native type double.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.