1

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.

Michael Westwort
  • 914
  • 12
  • 24
  • https://stackoverflow.com/questions/48212254/set-the-precision-for-decimal-numbers-in-c-sharp has a similar title but deals with changing the precision globally. – Michael Westwort Aug 26 '19 at 17:26
  • 2
    How is your database being accessed? Given they're the same (just with different precision), sending up a decimal (datatype) to your DB should work fine. The .00 is just precision on that type, and if you're storing via SQL with parameterisation it's not clear why it wouldn't work. – Rudi Visser Aug 26 '19 at 17:28
  • The database is accessed via a WSDL web service. The value that is sent to the database is of type decimal for sure. – Michael Westwort Aug 26 '19 at 17:35
  • 1
    Can you post the relevant WSDL? Is your XS type specified as decimal? – Tom Aug 26 '19 at 17:45
  • 1
    What @Tom said - The WSDL can't be right if it's expecting specific precision from the actual decimal datatype. If it's expecting a string representation of the decimal, then just format it accordingly before calling your WS. – Rudi Visser Aug 26 '19 at 17:55

2 Answers2

2

The Decimal data type stores its precision and you can change the precision without converting to string using arithmetic, e.g.:

decimal d = 10M;
decimal d00 = d * 1.00M;
Console.WriteLine(d00); // 10.00

See also Adjusting decimal precision, .net

If you want to get "at least N decimals", you can add a zero value with the relevant number of decimals using Decimal.Add, e.g.

decimal d0 = 10m;
decimal d1 = 10.0m;
decimal d2 = 10.00m;
decimal d3 = 10.000m;

Console.WriteLine(Decimal.Add(d0, 0.00m)); // 0.00
Console.WriteLine(Decimal.Add(d1, 0.00m)); // 0.00
Console.WriteLine(Decimal.Add(d2, 0.00m)); // 0.00
Console.WriteLine(Decimal.Add(d3, 0.00m)); // 0.000
Joe
  • 122,218
  • 32
  • 205
  • 338
  • I see. But this changes the precision *by* a certain number of decimals, not *to* a certain number of decimals. E.g. 10.00m * 1.00m yields 10.0000m. – Michael Westwort Aug 27 '19 at 07:48
  • @Michael - You can use `Math.Round` to round the result to a specific number of decimals, or if you want "at least N decimals", see update. – Joe Aug 27 '19 at 09:25
1

"How can I change aDecimal to 10.00 and bDecimal to 10?"

I'm not sure if this is exactly what you're looking for, but you could always re-assign the numbers using decimal.Parse after formatting the precision with a format string:

decimal aDecimal = 10m;
decimal bDecimal = 10.00m;

// Convert them to each other's values using a format string and `decimal.Parse`:
aDecimal = decimal.Parse($"{aDecimal:0.00}");
bDecimal = decimal.Parse($"{bDecimal:0}");
Rufus L
  • 36,127
  • 5
  • 30
  • 43