473,385 Members | 2,004 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Rounding decimal

Hi all,
I have to write a snippet that performs a rounding of decimal value, follow
this rules:

1.49m -1m
1.5 -2m
1.51 -2m

-1.49 --1m
-1.50 --2

I was trying this:

Math.Ceiling((-voceValue.Value ?? 0m) - 0.5M);

but not works correctly.

Has anyone idea?

Thanks a lot.
--
Luigi
http://blogs.dotnethell.it/ciupaz/
Nov 6 '08 #1
3 2076
After serious thinking Luigi wrote :
Hi all,
I have to write a snippet that performs a rounding of decimal value, follow
this rules:

1.49m -1m
1.5 -2m
1.51 -2m

-1.49 --1m
-1.50 --2

I was trying this:

Math.Ceiling((-voceValue.Value ?? 0m) - 0.5M);

but not works correctly.

Has anyone idea?

Thanks a lot.
Math.Round has an overload that accepts a MidpointRounding value. You
can set that to "ToEven" or "AwayFromZero". I guess you need the second
value.

Hans Kesting
Nov 6 '08 #2
A small correction for negative values.
These are the rules:

-1.5 = - 1

- 1.6 = - 2

-1.4 = - 1
L
Nov 6 '08 #3
"Hans Kesting" wrote:
After serious thinking Luigi wrote :
Hi all,
I have to write a snippet that performs a rounding of decimal value, follow
this rules:

1.49m -1m
1.5 -2m
1.51 -2m

-1.49 --1m
-1.50 --2

I was trying this:

Math.Ceiling((-voceValue.Value ?? 0m) - 0.5M);

but not works correctly.

Has anyone idea?

Thanks a lot.

Math.Round has an overload that accepts a MidpointRounding value. You
can set that to "ToEven" or "AwayFromZero". I guess you need the second
value.
Ok Hans, I'll checking.

Luigi
Nov 6 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Norvin Laudon | last post by:
Hi, Can somebody explain the following, from the MSDN documentation for the "System.Convert.ToInt32(double)" function <quote> Return Value value rounded to the nearest 32-bit signed...
4
by: vooose | last post by:
Consider a rounding up function: public static decimal RoundUp(decimal val, decimal round) { return ((decimal)Math.Ceiling((double)(val/round)))*round; } Math.Ceiling (and Math.Floor for...
4
by: spebola | last post by:
I am using vb.net 2003 professional and I get the following results when using the round method: dim Amount as decimal = 180.255 Amount = Amount.Round(Amount, 2) Amount now contains 180.25. ...
11
by: cj | last post by:
Lets assume all calculations are done with decimal data types so things are as precise as possible. When it comes to the final rounding to cut a check to pay dividends for example in VB rounding...
6
by: shaqattack1992-newsgroups | last post by:
Hello Everyone, I'm using the following on a form in my database: =(-Int(-Sum((IIf(=Yes,((*)+()), (((*)+)*1.06))*100))))/100 In this case, I want to calculate a total for an order. If...
29
by: Marco | last post by:
Hello, I have : float f = 36.09999999; When I do : char cf; sprintf(cf,"%0.03lf", f); I get : 36.100
5
by: Spoon | last post by:
Hello everyone, I don't understand how the lrint() function works. long lrint(double x); The function returns the nearest long integer to x, consistent with the current rounding mode. It...
206
by: md | last post by:
Hi Does any body know, how to round a double value with a specific number of digits after the decimal points? A function like this: RoundMyDouble (double &value, short numberOfPrecisions) ...
20
by: jacob navia | last post by:
Hi "How can I round a number to x decimal places" ? This question keeps appearing. I would propose the following solution #include <float.h> #include <math.h>
30
by: bdsatish | last post by:
The built-in function round( ) will always "round up", that is 1.5 is rounded to 2.0 and 2.5 is rounded to 3.0. If I want to round to the nearest even, that is my_round(1.5) = 2 # As...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.