Thread: Help with rounding a number

  1. #1
    Registered User
    Join Date
    May 2004

    Help with rounding a number

    Hi, I have a math project in which i need to round numbers correctly (ie. not just rounding up, but proper rounding, so if it was 4.4 it would become 4, and if it was 4.5 it would be 5). Now its easy enough to convert a float to an int, but then it allways rounds up, is it possible to somehow round it correctly?

    Thanks, Nick

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    How many decimal places do you want to round? I won't give you any code, because that would simply give you the answer to your homework. We don't do that. Just think about the problem:
    1) Read the decimal place.
    2) Round up if higher than N, otherwise round down.

    It's really not that hard.

    Hope is the first step on the road to disappointment.

  3. #3
    I agree,

    It's not hard. It can be done in one line of code, but I'm here on quzah's side because I want "to make them think" - by Quzah

    Yeah, I like that phrase in your signature. Now maybe to help with a few hints you can use the math header <math.h> possibly using abs, floor, ceil or even seperately modf, etc...

    Anyway, here is a link to all the codes given in the header file math.h

    Hope this helps,
    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  4. #4
    Registered User
    Join Date
    Apr 2004
    there is one function in math.h that exactly answers ur requirements. it is called "round" itself. i am not sure but as far as i know it is a standard function. why dont u just check it out
    even a fish would not have been in danger had it kept its mouth shut.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  2. scanf oddities
    By robwhit in forum C Programming
    Replies: 5
    Last Post: 09-22-2007, 01:03 AM
  3. Finding a number within a number
    By jeev2005 in forum C Programming
    Replies: 2
    Last Post: 01-10-2006, 08:57 PM
  4. help with a source code..
    By venom424 in forum C++ Programming
    Replies: 8
    Last Post: 05-21-2004, 12:42 PM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM