471,330 Members | 1,823 Online

# How to prevent roundiing in NumericUpDown

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

Jan 4 '06 #1
4 3439 On 4 Jan 2006 03:37:29 -0800, "Avi" <av********@gmail.com> wrote:
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
Jan 5 '06 #2
Sorry, I will correct myself.
"I want the value to remain like 0.069", that is 3 decimal places only.

Thanks

Jan 5 '06 #3
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
ja***@removethis.dystopia.fi
http://www.saunalahti.fi/janij/
Jan 5 '06 #4
That's exactly what a want.

Thanks a lot.

Jan 6 '06 #5

### This discussion thread is closed

Replies have been disabled for this discussion.