# decimal value to long without rounding the amount

• ### Question

• User2012297617 posted

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.

Thursday, August 30, 2018 2:41 AM

• User2012297617 posted

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;

• Marked as answer by Thursday, October 7, 2021 12:00 AM
Friday, August 31, 2018 2:01 PM

### All replies

• User753101303 posted

Hi,

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).

Thursday, August 30, 2018 11:15 AM
• User2012297617 posted

Hello,  I found the Money class here:   https://github.com/square/connect-csharp-sdk/blob/master/src/Square.Connect/Model/Money.cs

Thank you

Thursday, August 30, 2018 12:55 PM
• User2012297617 posted

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;

• Marked as answer by Thursday, October 7, 2021 12:00 AM
Friday, August 31, 2018 2:01 PM
• User753101303 posted

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).

Friday, August 31, 2018 3:41 PM
• User2012297617 posted

Hi PatriceSc,