Thread overview | |||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
January 30, 2015 How to make a Currency class from std.BigInt? | ||||
---|---|---|---|---|
| ||||
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"); } |
January 30, 2015 Re: How to make a Currency class from std.BigInt? | ||||
---|---|---|---|---|
| ||||
Posted in reply to RuZzz | What do I need to learn? |
January 30, 2015 Re: How to make a Currency class from std.BigInt? | ||||
---|---|---|---|---|
| ||||
Posted in reply to RuZzz | 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 convert expression (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"]). |
January 31, 2015 Re: How to make a Currency class from std.BigInt? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ivan Kazmenko | 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); |
January 31, 2015 Re: How to make a Currency class from std.BigInt? | ||||
---|---|---|---|---|
| ||||
Posted in reply to RuZzz | without to!string |
January 31, 2015 Re: How to make a Currency class from std.BigInt? | ||||
---|---|---|---|---|
| ||||
Posted in reply to RuZzz | 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;
}
}
|
January 31, 2015 Re: How to make a Currency class from std.BigInt? | ||||
---|---|---|---|---|
| ||||
Posted in reply to RuZzz | Thanks. Do I need to use rational fractions for the Currencies class? |
January 31, 2015 Re: How to make a Currency class from std.BigInt? | ||||
---|---|---|---|---|
| ||||
Posted in reply to RuZzz | I want to understand the correct architecture of the class. |
January 31, 2015 Re: How to make a Currency class from std.BigInt? | ||||
---|---|---|---|---|
| ||||
Posted in reply to RuZzz | 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.
|
January 31, 2015 Re: How to make a Currency class from std.BigInt? | ||||
---|---|---|---|---|
| ||||
Posted in reply to RuZzz | The next version, which does not work: https://bpaste.net/show/b9c85de68d07 |
Copyright © 1999-2021 by the D Language Foundation