How to prevent roundiing in NumericUpDown

  • Thread starter Thread starter Avi
  • Start date Start date
A

Avi

Hello All,
I am using NumericUpDown control. I have set Decimal places property to

3.
Problem is that when I programatically assign it a value which has 4
digits after decimal point ,
it rounds the value to 3 digits. E.g. 0.0695 --> 0.070
How can I prevent this rounding? I want the value to remain like 0.0695

--> 0.069.


Thanks
 
Hello All,
I am using NumericUpDown control. I have set Decimal places property to

3.
Problem is that when I programatically assign it a value which has 4
digits after decimal point ,
it rounds the value to 3 digits. E.g. 0.0695 --> 0.070
How can I prevent this rounding? I want the value to remain like 0.0695

--> 0.069.


Thanks

Set decimal place property to 4?

Otis Mukinfus
http://www.otismukinfus.com
http://www.tomchilders.com
 
Sorry, I will correct myself.
"I want the value to remain like 0.069", that is 3 decimal places only.

Thanks
 
Avi,
Problem is that when I programatically assign it a value which has 4
digits after decimal point, it rounds the value to 3 digits.
How can I prevent this rounding?

Since you are programmatically setting the new value for the NumericUpDown
control, you are free to round/truncate the value *before* you assign it to
the Value property of the NumericUpDown control.

That is, in your code you are probably doing something like this:

---------------
decimal myValue = 0.0695m; // some calculation here
numericUpDown1.Value = myValue;
---------------

Now, instead of directly assigning the result of your calculation to the
Value property and let the control round the number for you, you would
simply round the result to your liking, in this case round down instead of
up.

To do so, you could use simple code like this:

---------------
decimal myValue = 0.0695m;
decimal myRoundedValue = System.Math.Truncate(myValue * 1000) / 1000;
numericUpDown1.Value = myRoundedValue;
---------------

Of course, this code might not be exactly what you want (as you didn't
specify exactly how you want to round), but something very similar should be
what you are after.

Hope this helps!

--
Regards,

Mr. Jani Järvinen
C# MVP
Helsinki, Finland
(e-mail address removed)
http://www.saunalahti.fi/janij/
 
Back
Top