473,396 Members | 1,836 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,396 software developers and data experts.

About round command?

I don't know how to write the code for round command?
For example if more than .5 then round up,if less than .5
round down such as 4.3 round to 4.0 and 5.5 round to 6.0
Nov 16 '05 #1
4 1725

There are lots of solutions to this.

If you would like to make your own round-function take a look at the modulo
function %.

Have you seen the
Math::Round(3.44, 1); //Returns 3.4.
Math::Round(3.45, 1); //Returns 3.4.
Math::Round(3.46, 1); //Returns 3.5.

in the .Net framework?
--
Regards,
Lars-Inge Tonnessen
http://emailme.larsinge.com
http://www.larsinge.com

Nov 16 '05 #2
Lars-Inge Tønnessen wrote:
There are lots of solutions to this.

If you would like to make your own round-function take a look at the modulo
function %.


% is an operator, not a function, and it is not defined for non-integral
types.

Gerhard Menzl

Nov 16 '05 #3
Sin
> I don't know how to write the code for round command?
For example if more than .5 then round up,if less than .5
round down such as 4.3 round to 4.0 and 5.5 round to 6.0


A simple trick is to add 0.5 to the value (if positive) or remove 0.5 (if
negative). By converting the value back into an integer it will
automatically round to the right value.

Ex:

4.0 + 0.5 = 4.5... truncated (cast) = 4.0
4.3 + 0.5 = 4.8... truncated (cast) = 4.0
4.5 + 0.5 = 5.0... truncated (cast) = 5.0
4.7 + 0.5 = 5.2... truncated (cast) = 5.0

Here's a macro that implements the "logic" :

#define ROUND_INT(x) ((int)((x)+(((x)>0)?0.5:-0.5)))

If you need it for a float/double, just cast back into one...

Alex.
Nov 16 '05 #4
This is where your programming creativity comes in when you develop your own
function/method/class. =:o)

--
Regards,
Lars-Inge Tonnessen
http://emailme.larsinge.com
http://www.larsinge.com
Nov 16 '05 #5

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

Similar topics

0
by: Tony Johansson | last post by:
Hello! Below is a small program using the adaptor pattern. We have four classes these are: PegAdapter, RoundPeg, SquarePeg and a class TestPegs acting like a client In class PegAdapter has...
14
by: m | last post by:
all, i am trying to use the function round() which I found through google to be declared in math.h ( http://www.gnu.org/software/libc/manual/html_node/Rounding-Functions.html). this function does...
9
by: Ronald W. Roberts | last post by:
I'm having a problem understanding the Round function. Below are quotes from two books on VB.NET. The first book shows examples with one argument and how it rounds. The second book something...
21
by: Karl O. Pinc | last post by:
FYI, It'd be nice if the error message from a REFERENCES constraint mentioned the column name into which the bad data was attempted to be inserted. In PostgreSQL 7.3: sandbox=> insert into...
4
by: Fuzzydave | last post by:
I have been using a round command in a few places to round a value to zero decimal places using the following format, round('+value+', 0) but this consistantly returns the rounded result of...
10
by: David Coleman | last post by:
I am running VS 2003 and have applied SP1. (On WinXP SP2, .Net 1.1) In the Command Window I get the following ? Math.Round(0.715, 2) 0.72 ? Math.Round(0.725, 2) 0.72 ? Math.Round(0.735, 2)...
14
by: ray | last post by:
Dear all, In my vb.net program, I have to round a decimal number to two decimal place. For example, for the expression "Decimal.Round(31* 1.555, 2)", I should get the result "48.21". However, the...
4
by: =?Utf-8?B?UmVuZQ==?= | last post by:
Hello everyone I have a problem with Math.Round, it´s ocurring some strange: Math.Round(12.985) = 12.98, it´s wrong. It should be: 12.99 Why?? What is the problem? Help ME !!!!
2
by: dkruger | last post by:
Hi Everyone, I am having a little problem that I am hoping someone can help me out with. In one of my scripts, I am working with some dollar values, and using round to change the result...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.