0

In MSSQL,
COST_C (float)

In Model,
[DisplayFormat(DataFormatString = "{0:0,0}", ApplyFormatInEditMode = true)] public double COST_C { get; set; }

In View,
<input name="COST_C" class="form-control" value="@ViewBag.costC" />

In Controller,
ViewBag.costC = Convert.ToDecimal(project.COST_);

When update COST_C in controller, the value on page after update become different.....
1000.00 become 1000
1000.20 become 1000.20001220703
1000.18 become 1000.17999267578

how can I make it exactly what it is?

Waller
  • 433
  • 1
  • 6
  • 20
  • 3
    Possible duplicate of [Is floating point math broken?](http://stackoverflow.com/questions/588004/is-floating-point-math-broken) – piet.t Aug 04 '16 at 07:34

1 Answers1

0

Thats a general problem with floating points. The accuracy of your value depends on the size of your number. If you have a big number you cant reliably express a high accuracy in decimal places. The datatype itself is limited and cant express every number.

If its an option for you (and you really need that accuracy), you can seperate the decimal places to in a new floating point variable to achieve higher accuracy.

If you just want to display the correct values you can format your value like %.2f, but you may need to round the value beforehand. You still arent 100% accurate though.

Sven D.
  • 23
  • 8