1

When I pass this object back as JSON, it looks like this:

0.000000000000000e+000

My code in C# is:

// get adjustments for user
IEnumerable<Severity> existingSeverities = 
    from s in db.AdjusterPricingGrossLossSeverities
    where s.type == type
    && s.adjusterID == adj.id
    select new Severity
    {
        id = s.severity,
        adjustment = Math.Round((double)s.adjustment, 2, MidpointRounding.AwayFromZero).ToString(),
        isT_E = (bool)s.isTimeAndExpense
    };

How can I make it just round to two decimal places (0.00)?

user1477388
  • 20,790
  • 32
  • 144
  • 264
  • 5
    Why? decimal places only matter when presenting to the user. – H H Jun 14 '13 at 13:23
  • It is rounding to 2 decimal places but it's still being stored as `double` which is by definition many more decimal places. You will need to present in JavaScript however you like as Henk says. – El Ronnoco Jun 14 '13 at 13:25
  • @ElRonnoco I am storing it as a string (`adjustment` is a string whereas `s.adjustment` is the double) – user1477388 Jun 14 '13 at 13:29
  • @HenkHolterman I am passing it as a string, not a double. So, the presentation matters here. – user1477388 Jun 14 '13 at 13:30
  • @GrantWinney `s.adjustment` is a `?double` (optional double). `adjustment`, however, is `string`. – user1477388 Jun 14 '13 at 13:30

4 Answers4

3

Use;

dec.ToString("#.##");

See this answer for more information

If it's a nullable double in a Console app do;

    double ? d = 2.22222;
    Console.WriteLine(d.Value.ToString("#.##"));
Community
  • 1
  • 1
Sam Leach
  • 12,746
  • 9
  • 45
  • 73
1

I think that you are confusing two things. The "real" number is not what you see. The real number is stored internally in a binary format. The decimal digits that you see do not exist in this internal format. What you see is the conversion of this value to a decimal representation as a string.

The conversion of any internal binary representation to a human visible string is called formatting. The Round function does not format. See this example:

double x = 0.123456000000000e+000;
double y = Math.Round(x, 2, MidpointRounding.AwayFromZero);
// y ====> 0.120000000000000e+000;

The rounding function changes the internal value. What you need is probably not to change the value but to display the unchanged value with only two digits:

string formattedValue = x.ToString("N2");

If you are deling with currencies, use decimal rather than double. decimal uses a binary encoded decimal format internally. Values like 1/10 cannot be represented precisely as binary number in a computer just like 1/7 cannot be represent precisely in decimal notation (0.142857142857...). But 1/10 has an exact internal representation when stored as a decimal.

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
  • Thank you, that makes sense. However, this wouldn't have worked as is (inline), ultimately, since LINQ to SQL works the way it does. The only solution that worked for me is the one I found and posted here http://stackoverflow.com/a/17109950/1477388 – user1477388 Jun 14 '13 at 13:53
0

Try this:

// get adjustments for user
IEnumerable<Severity> existingSeverities = 
from s in db.AdjusterPricingGrossLossSeverities
where s.type == type
&& s.adjusterID == adj.id
select new Severity
{
    id = s.severity,
    adjustment = s.adjustment.GetValueOrDefault().ToString("0.##"),
    isT_E = (bool)s.isTimeAndExpense
 };

-Edit-

I think that maybe you will need to have the Severity class have a Property that takes a double and saves a string to Severity.adjustment, like so:

 Severity
 {
      //rest of class as normal

      public double SetAdjustment
           {
                set { adjustment = value.ToString("0.00"); } }
           }
 }

-Edit, part 2-

// get adjustments for user
IEnumerable<Severity> existingSeverities = 
from s in db.AdjusterPricingGrossLossSeverities
where s.type == type
&& s.adjusterID == adj.id
select new Severity
{
    id = s.severity,
    SetAdjustment = s.adjustment.GetValueOrDefault(),
    isT_E = (bool)s.isTimeAndExpense
 };

The rest of your code should not need to be changed, it should still use (Severity variable).adjustment as normal. This is just to get around the fact that there is not guaranteed way to translate .Net's Standard Numeric Format Strings into SQL's Convert, much less any custom formatting.

Tory
  • 520
  • 3
  • 13
0

Turns out, this was a LINQ to SQL issue. I did this, and it works...

// get adjustments for user
IEnumerable<Severity> existingSeverities = 
    from s in db.AdjusterPricingGrossLossSeverities
    where s.type == type
    && s.adjusterID == adj.id
    select new Severity
    {
        id = s.severity,
        adjustment = roundtest(s.adjustment.GetValueOrDefault()),
        isT_E = (bool)s.isTimeAndExpense
    };

// test method...
public string roundtest(double num)
{
    return num.ToString("#.##");
}
user1477388
  • 20,790
  • 32
  • 144
  • 264