1

This is a simple question for sure, but I can't find the solution. It's the opposite of this question. In my case, I have a decimal and a string properties on my ViewModel, like this:

public string FactorText 
{
    get
    {
        if (this.Factor != 0)
        {
            return this.Factor.ToString();
        }
    }

    set
    {
        this._factorText = value;
    }
}

public decimal Factor { get; set; }

When the .ToString() acts, it fills the number with zeroes at the right, e.g:

  • 1.1 becomes 1.100000000000000
  • 1.9999999999 becomes 1.999999999900000

... and so on. It fills the remaing of 15 decimal places with zeroes.

I don't really know why and if it have a way to make sure .ToString() stops making that.

If this information is relevant, that Factor is mapped with Fluent NHibernate as follows:

Map((x) => x.Factor).Column("FACTOR").Precision(30).Scale(15).Not.Nullable();

I don't know if this can be the cause of that behaviour.

Thank you in adv.

Update: I just can't change the scale and precision. They match the column on the database definition that is Decimal(30,15).

Community
  • 1
  • 1
DontVoteMeDown
  • 21,122
  • 10
  • 69
  • 105

5 Answers5

2

It's likely that this is due to how the database is representing the number. For example, if you insert 1.1 in a SQL Server 2008 decimal(30,15) field and select the result, you will get back 1.100000000000000.

See the comments below, the accepted solution was to use .ToString("G29") as found in How to format a decimal without trailing zeros

Community
  • 1
  • 1
Jamie Ide
  • 48,427
  • 16
  • 81
  • 117
  • I have suspected that. But I'm could not find out how to prevent that on the mapping or on the .ToString function. – DontVoteMeDown Nov 12 '13 at 18:16
  • This question offers a few options: http://stackoverflow.com/questions/17516460/how-to-format-a-decimal-without-trailing-zeros. Most applications use a format string on the page/form/viewmodel to display the desired decimal places. If you just want to remove the trailing zeros, you'll probably have to manipulate the string returned by the ToString call. – Jamie Ide Nov 12 '13 at 18:41
  • That was what I'm after, it worken with `.ToString("G29")`. Update your answer with this code, and I'll accept. – DontVoteMeDown Nov 12 '13 at 19:10
  • Really interesting solution. But/And the most interesting is, how it proves again the fact: calling `ToString` without params should be avoided – Radim Köhler Nov 13 '13 at 03:32
1

1.1 becomes 1.100000000000000

              ^^^^^^^^^^^^^^^ 15 places

The .Scale(15) is what is causing this behavior. Reduce this to the number of decimal places you require.

gleng
  • 6,185
  • 4
  • 21
  • 35
  • The problem is that I need that scale to be 15. I can't reduce it. It is equal to column definition in database: `Decimal(30,15)`. – DontVoteMeDown Nov 12 '13 at 18:12
0

You can use string.format-Parameters like

string.format("{0:0.0000}",yourvalue");

Ole Albers
  • 8,715
  • 10
  • 73
  • 166
0

Maybe this works for you:

if (this.Factor.Length < 15) for (int i = this.Factor.Length; i <= 15; i++) this.Factor += "0";

Regards.,

k

EDIT: Sorry, try this:

return Convert.ToDecimal(this.Factor, CultureInfo.InvariantCulture).ToString();
kintaro
  • 2,393
  • 2
  • 16
  • 16
0

Just remove Scale(15) to let the original value be loaded.

mehmet mecek
  • 2,615
  • 2
  • 21
  • 25