Last post Sep 01, 2018 12:11 PM by lesponce
Aug 30, 2018 02:41 AM|lesponce|LINK
I got this line of code where the amount is a long field. The hardcoded 100 is capture as $1.00
Money amount = new Money(100, Money.CurrencyEnum.USD);
This is how is declared as I can see in the metadata:
public Money(long? Amount = default(long?), CurrencyEnum? Currency = default(CurrencyEnum?));
My dilema is that I got a decimal amount which is part of the code. This value holds the real amount.
How do I convert my decimal amount to long so I can pass it as expected?
I guess, if 4.99 is the decimal amount, I guess is suppossed to be set as 499. Right?
So if the code is...
decimal DecimalAmount // set to 4.99
// NewField should be the coverted value of 4.99 to 499
Money amount = new Money(NewField, Money.CurrencyEnum.USD);
Do I have to convert it to double first? If so, I see that is set as 4. I don't want to have the amount rounded.
Aug 30, 2018 11:15 AM|PatriceSc|LINK
And this Money class comes from where ? Don't you have some documentation for it ?
Sometimes the currency amount is scaled depending on the currency so that all values are stored using an integer rather than a decimal data type (so 123 is 1.23 $ but might be 0.123 or 123000 for other currencies).
Aug 30, 2018 12:55 PM|lesponce|LINK
Hello, I found the Money class here: https://github.com/square/connect-csharp-sdk/blob/master/src/Square.Connect/Model/Money.cs
Aug 31, 2018 02:01 PM|lesponce|LINK
I resolved my issue by doing this:
DecAmt = 1.74m;
string str = DecAmt.ToString().Replace(".", string.Empty);
decimal dec = decimal.Parse(str);
LongAmt = (long)dec;
Aug 31, 2018 03:41 PM|PatriceSc|LINK
Don't !!!!! It's just a $ to cents conversion ie you have to use cents=dollars*100. With 1.2 you'll get 12 when it should be 120.
If you want to handle any currency it could be worh to make sure it uses 2 decimals. It should be ok for most but when doing worldwide stuff you sometimes run into edge cases that are looking strange to you. I'll perhaps do a quick check (you should have
that in CultureInfo).
Edit: I'm telling that because of a "The amount of money, in the smallest denomination of the currency" comment found in their code so basically they are just using the smallest monetary unit (ie for USD they are using amounts in cents rather than
in dollars). Make sense with what you saw and with what I saw once in an accounting software (ue they are using integer values because they are just using the smallest monetary unit for the currency ie here cents rather than dollars).
Sep 01, 2018 12:11 PM|lesponce|LINK
Thanks for your feedback. I will check into your suggestion.