## digitalmars.D.learn - How to make a Currency class from std.BigInt?

- RuZzz (54/54) Jan 30 2015 module axfinance.api.currencies;
- RuZzz (1/1) Jan 30 2015 What do I need to learn?
- Ivan Kazmenko (7/9) Jan 30 2015 expression (1) of type double to
- RuZzz (2/2) Jan 31 2015 Thanks. Do I need to use rational fractions for the Currencies
- RuZzz (1/1) Jan 31 2015 I want to understand the correct architecture of the class.
- Ivan Kazmenko (5/6) Jan 31 2015 Sorry, you still did not state your problem (or what you are
- RuZzz (2/2) Jan 31 2015 The next version, which does not work:
- Colin (4/6) Jan 31 2015 I really dont understand what your trying to achieve here.
- RuZzz (5/7) Jan 31 2015 Maybe this?
- RuZzz (2/2) Jan 31 2015 Maybe I need good examples on C++, for this class, because a lot
- RuZzz (1/1) Jan 31 2015 I am looking for not only free solutions.
- RuZzz (2/2) Jan 31 2015 The next version
- RuZzz (10/10) Feb 11 2015 With "eris" lib some problems, the first error:
- RuZzz (1/1) Feb 11 2015 https://github.com/acmeism/RosettaCodeData/blob/master/Task/Arithmetic-R...
- RuZzz (8/8) Feb 14 2015 The next version https://bpaste.net/show/dc3c5f10f2ca
- Jay Norwood (7/7) Feb 15 2015 This library allow to specify the internal base of the arbitrary

module axfinance.api.currencies; import std.array, std.stdio, std.system, std.bigint, std.conv; class Currencies { public: this(ulong pf) { exponent(pf); } bool integer(BigInt pb) { zInt = pb; return true; } string value() { string res = to!string(zInt); insertInPlace(res, res.length - zExp,"."); return res; } private: bool exponent(ulong pe) { zExp = pe; return true; } BigInt zInt; ulong zExp; } unittest { double d1 = 45.67; immutable LIM_FIAT = 2, LIM_EXCH = 6, LIM_CRYP = 8; auto c = [ "BTC":["N-01":new Currencies(LIM_CRYP), "N-02":new Currencies(LIM_CRYP), "SUM1":new Currencies(LIM_CRYP)], "CNY":["N-01":new Currencies(LIM_FIAT), "N-02":new Currencies(LIM_EXCH), "N-03":new Currencies(LIM_CRYP), "SUM1":new Currencies(LIM_CRYP), "SUM2":new Currencies(LIM_CRYP)]]; // EUR, LTC, RUB; c["BTC"]["N-01"] = 1.00000002;//Error: cannot implicitly convert expression (1) of type double to axfinance.api.currencies.Currencies c["BTC"]["N-02"] = 15.00000002+"13455565.45665435"; c["BTC"]["SUM1"] = c["BTC"]["N-01"] + c["BTC"]["N-02"]; assert(c["BTC"]["SUM1"] == "13455581.45665437"); c["CNY"]["N-01"] = 1.02; c["CNY"]["N-02"] = "0.01"; c["CNY"]["N-03"] = "0.00000001"; c["CNY"]["SUM1"] = c["CNY"]["N-01"] + c["CNY"]["N-02"]; assert(c["CNY"]["SUM1"] == "1.03"); assert(to!double(c["CNY"]["SUM1"]) == 1.03); c["CNY"]["SUM2"] = c["CNY"]["N-01"] + c["CNY"]["N-03"]; assert(c["CNY"]["SUM2"] == "1.02000001"); }

Jan 30 2015

On Friday, 30 January 2015 at 20:34:53 UTC, RuZzz wrote:What do I need to learn?c["BTC"]["N-01"] = 1.00000002;//Error: cannot implicitly convertexpression (1) of type double to axfinance.api.currencies.Currencies As I see it, there is no constructor in your class with a double argument. So you cannot assign a double (1.00000002) to a Currencies struct (c["BTC"]["N-01"]).

Jan 30 2015

How to get amount of digit after point in a double for to get integer from a double? assert(amountAfterPoint(1.456) == 3); assert(amountAfterPoint(0.00006) == 5);

Jan 31 2015

On Saturday, 31 January 2015 at 12:07:23 UTC, RuZzz wrote:How to get amount of digit after point in a double for to get integer from a double? assert(amountAfterPoint(1.456) == 3); assert(amountAfterPoint(0.00006) == 5);How about a loop like import std.stdio; void main(){ writefln("%s", 1.45.numDigits);// prints 2 writefln("%s", 1.452343.numDigits);// prints 6 writefln("%s", 1.0.numDigits);// prints 0 } long numDigits(double num){ long i=0; double n=num; while(true){ if(n - (cast(long)n) == 0){ return i; } i++; n *= 10; } }

Jan 31 2015

Thanks. Do I need to use rational fractions for the Currencies class?

Jan 31 2015

I want to understand the correct architecture of the class.

Jan 31 2015

On Saturday, 31 January 2015 at 13:45:22 UTC, RuZzz wrote:I want to understand the correct architecture of the class.Sorry, you still did not state your problem (or what you are trying to achieve) clearly. Writing down a clear problem description is likely to get you halfway to the solution.

Jan 31 2015

The next version, which does not work: https://bpaste.net/show/b9c85de68d07

Jan 31 2015

On Saturday, 31 January 2015 at 14:26:45 UTC, RuZzz wrote:The next version, which does not work: https://bpaste.net/show/b9c85de68d07I really dont understand what your trying to achieve here. Maybe you can articulate it in one post what this class is trying to achieve?

Jan 31 2015

On Saturday, 31 January 2015 at 15:02:09 UTC, Colin wrote:Maybe you can articulate it in one post what this class is trying to achieve?Maybe this? https://github.com/andersonpd/decimal/blob/master/decimal/bigfloat.d The main thing is not to lose currency in calculations that use arithmetic.

Jan 31 2015

Maybe I need good examples on C++, for this class, because a lot of examples on C++.

Jan 31 2015

On 31.01.15 15:45, RuZzz wrote:Maybe I need good examples on C++, for this class, because a lot of examples on C++.Maybe this can help you: https://github.com/andersonpd/decimal

Jan 31 2015

On 31.01.15 15:56, zeljkog wrote:On 31.01.15 15:45, RuZzz wrote:Also http://code.dlang.org/packages/erisMaybe I need good examples on C++, for this class, because a lot of examples on C++.Maybe this can help you: https://github.com/andersonpd/decimal

Jan 31 2015

The next version https://bpaste.net/show/9468f24d9df0

Jan 31 2015

With "eris" lib some problems, the first error: .../.dub/packages/eris-0.0.1/eris/integer/digits.d(241): Error: cannot implicitly convert expression (digits.length) of type ulong to int What can I import to use rational numbers? I found it https://github.com/dsimcha/Rational/blob/master/rational.d https://github.com/d-gamedev-team/gfm/blob/master/math/gfm/math/rational.d but I do not understand what the story ended with rational numbers in D.

Feb 11 2015

https://github.com/acmeism/RosettaCodeData/blob/master/Task/Arithmetic-Rational/D/arithmetic-rational.d

Feb 11 2015

The next version https://bpaste.net/show/dc3c5f10f2ca I use https://github.com/dsimcha/Rational/blob/master/rational.d I want to do it: auto c = new Currencies!Rational.rational.Rational!(BigInt); where Rational.rational.Rational it is std.rational.Rational I get the error: Error: template instance Currencies!(Rational) does not match template declaration Currencies(T)

Feb 14 2015

This library allow to specify the internal base of the arbitrary precision numbers( default is decimal), as well as allows specification of the precision of floating point values. Each floating point number precision can be read with .precision(). Also supports specification of rounding modes. Seems like it would be a nice project for a port to D. http://www.hvks.com/Numerical/arbitrary_precision.html

Feb 15 2015