rounding numbers

This is a discussion on rounding numbers within the C Programming forums, part of the General Programming Boards category; I am trying to write a program which first gets a number from the user, then rounds the number to ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    2

    rounding numbers

    I am trying to write a program which first gets a number from the user, then rounds the number to an integer, then to its 10s place, 100s place and finally its 1,000s place. Here's what Iíve got so far:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    float	getNumber();
    float	roundToInteger(float);
    float	roundToTenths(float);
    float	roundToHundredths(float);
    float	roundToThousandths(float);
    
    int main(){
    	float	number;
    	float	to_integer;
    	float	to_tenths;
    	float	to_hundredths;
    	float	to_thousandths;
    
    	number = getNumber();
    
    	while(number != -1){
    		to_integer = roundToInteger(number);
    		to_tenths = roundToTenths(number);
    		to_hundredths = roundToHundredths(number);
    		to_thousandths = roundToThousandths(number);
    
    		printf("\tnumber rounded to integer: %f\n", to_integer);
    		printf("\tnumber rounded to tenths: %f\n", to_tenths);
    		printf("\tnumber rounded to hundreths: %f\n", to_hundredths);
    		printf("\tnumber rounded to thousandths: %f\n\n", to_thousandths);
    		number = getNumber();
    	}
    
    	return 0;
    }
    
    float	getNumber(){
    	float	number;
    
    	printf("enter a number (-1 to end): ");
    	scanf("%f", &number);
    	return number;
    }
    
    
    float	roundToInteger(float number){
    	return floor(number + .5);
    }
    
    float	roundToTenths(float number){
    	return floor(number * 10 + .5) / 10;
    }
    
    float	roundToHundredths(float number){
    	return floor(number * 100 + .5) / 10;
    }
    
    float	roundToThousandths(float number){
    	return floor(number * 1000 + .5) / 10;
    }
    After compiling, I get the following for output:
    enter a number (-1 to end): 123.456789
    number rounded to integer: 123.000000
    number rounded to tenths: 123.500000
    number rounded to hundreths: 1234.599976
    number rounded to thousandths: 12345.700195

    enter a number (-1 to end):
    Thank you in advance.

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    83

    Try Following Code

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    float	getNumber();
    float	roundToInteger(float);
    float	roundToTenths(float);
    float	roundToHundredths(float);
    float	roundToThousandths(float);
    
    int main(){
    	float	number;
    	float	to_integer;
    	float	to_tenths;
    	float	to_hundredths;
    	float	to_thousandths;
    
    	number = getNumber();
    
    	while(number != -1){
    		to_integer = roundToInteger(number);
    		to_tenths = roundToTenths(number);
    		to_hundredths = roundToHundredths(number);
    		to_thousandths = roundToThousandths(number);
    
    		printf("\tnumber rounded to integer: %f\n", to_integer);
    		printf("\tnumber rounded to tenths: %f\n", to_tenths);
    		printf("\tnumber rounded to hundreths: %f\n", to_hundredths);
    		printf("\tnumber rounded to thousandths: %f\n\n", to_thousandths);
    		number = getNumber();
    	}
    
    	return 0;
    }
    
    float	getNumber(){
    	float	number;
    
    	printf("enter a number (-1 to end): ");
    	scanf("%f", &number);
    	return number;
    }
    
    
    float	roundToInteger(float number){
    	return floor(number + .5);
    }
    
    float	roundToTenths(float number){
    	return floor(number * 100 + .5) / 10;
    }
    
    float	roundToHundredths(float number){
    	return floor(number * 1000 + .5) / 10;
    }
    
    float	roundToThousandths(float number){
    	return floor(number * 10000 + .5) / 10;
    }

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,676
    >> Try Following Code
    Closer....

    maple23 fixed up your multiply factors. Now you just have to fix the divide factors to "re-base" the value correctly.

    You will notice a small loss of precision in your printout. That's because 123.456789 can't be represented as precisely in a float as it can in a double. As a float, the debugger shows it as 123.45679.

    Don't ignore your compiler warnings. Changing all your floats to double will remove the warnings and the (very small) loss of precision when dealing with 123.456789.

    gg

  4. #4
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    Is this what you want?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    float	getNumber();
    float	roundToInteger(float);
    float	roundToTenths(float);
    float	roundToHundredths(float);
    float	roundToThousandths(float);
    
    int main(){
    	float	number;
    	float	to_integer;
    	float	to_tenths;
    	float	to_hundredths;
    	float	to_thousandths;
    
    	number = getNumber();
    
    	while(number != -1){
    		to_integer = roundToInteger(number);
    		to_tenths = roundToTenths(number);
    		to_hundredths = roundToHundredths(number);
    		to_thousandths = roundToThousandths(number);
    
    		printf("\tnumber rounded to integer: %f\n", to_integer);
    		printf("\tnumber rounded to tenths: %f\n", to_tenths);
    		printf("\tnumber rounded to hundreths: %f\n", to_hundredths);
    		printf("\tnumber rounded to thousandths: %f\n\n", to_thousandths);
    		number = getNumber();
    	}
    
    	return 0;
    }
    
    float getNumber(){
    	float	number;
    
    	printf("enter a number (-1 to end): ");
    	scanf("%f", &number);
    	return number;
    }
    
    
    float roundToInteger(float number){
    	return round(number);
    }
    
    float roundToTenths(float number){
    	return round(number * 10) / 10;
    }
    
    float roundToHundredths(float number){
    	return round(number * 100) / 100;
    }
    
    float roundToThousandths(float number){
    	return round(number * 1000) / 1000;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  2. Newbie question: rounding numbers
    By cantore in forum C Programming
    Replies: 10
    Last Post: 02-04-2006, 02:24 PM
  3. (C++) How would you go about rounding numbers?
    By jeffcoulter in forum C++ Programming
    Replies: 5
    Last Post: 09-19-2005, 07:47 AM
  4. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 10:15 AM
  5. Rounding Numbers accurately
    By crystaldawn68 in forum C++ Programming
    Replies: 5
    Last Post: 10-06-2001, 02:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21