I need to convert a decimal value into something like
0.3 = USDC000000000030
The code i wrote is as below.
transactionAmount = String.Format("USDC{0}", ((int)(amount*100)).ToString("D12"));
However, it is working for some values perfectly like the example given above. For other some values it is conversion is wrong for example
0.9 = USDC000000000089
1 = USDC000000000099
1.1 USDC000000000109
Any idea why is it happening so?
Thanks in advance
You’re probably using
double, which for financial values is a huge mistake.Use
decimalinstead, and wash your troubles away.To wit:
Output:
Note that the language specification states: