I convert two strings into decimals:
using System;
class MainClass {
public static void Main (string[] args) {
string aString = "10";
decimal aDecimal = System.Convert.ToDecimal(aString);
Console.WriteLine((decimal)aDecimal);
string bString = "10.00";
decimal bDecimal = System.Convert.ToDecimal(bString);
Console.WriteLine((decimal)bDecimal);
}
}
The first output yields 10, the second one 10.00.
How can I change aDecimal
to 10.00 and bDecimal
to 10? (Please note: I am referring to the decimals, not to the output string representation.)
For bDecimal
, Math.Round(bDecimal,0)
works; this might be a good solution. For aDecimal
, aDecimal = aDecimal + 0.01m - 0.01m
works; this might be a good workaround for most cases but not the real solution.
The background of my question is that I want to transfer the numbers into a third-party database. The database only accepts decimals but not integers. 10 (although it is of type decimal in C#) is considered an integer whereas 10.00 is not.