I have a NET6 microservice that interpolate decimal variables truncating the decimal portion when the decimal part is 0, so assuming that d is my decimal variable with value 5.0:
$"My decimal is {d}"
result converted as "My decimal is 5" The same instruction in a AzureFunction v4 with NET6 result converted as "My decimal is 5.0".
In both of their Startup classes I have set the default culture in the Configure as follows:
CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("en-US");
CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo("en-US");
What I would expect is to have the same result. Am I missing something?
The
decimalstructure contains an exponent, sign bit, and "value". The "value" is across threeintinternally, the other information is in a "flags"int. This is not so different from IEEE float, except decimal uses base 10 exponents.Relevant source code
So for example, as MySkullCaveIsADarkPlace says, if you have a decimal
d_5 = decimal.Parse("5"), and ad_5_00 = decimal.Parse("5.00"), these will have different internal structure.For the curious, you can retrieve these values using reflection
result:
If you want a specific number of decimal places, then call with a string format (e.g.,
$"{d_5:N2}"->5.00), or useDecimal.Truncatefor no decimal places. Otherwise the defaultToStringwill use the available information (flags, hi, mid, lo) in the object.