# trouble with my first c program

Printable View

• 01-27-2005
mblazar
trouble with my first c program
Hi, this is my first post on these forums, I searched through the faq and couldnt find an answer so I am asking here. Im not even sure if its possible, but I am trying to make it so that my program won't round the result when its printed to only two decimal places on the screen. Thanks in advance for any help. :D

Code:

```/* Programmer: Michael Blazar   Objective:To find out the cost of wallpapering a spherical room with a diameter of 6084 and the cost of the wallpaper input by the user */ #include <stdio.h> #define PI  3.14159265 int main(void) {   float cost;     int DIAMETER = 6084;       printf("What is the cost of wallpaper per square meter in dollars?\n");     scanf("%f", &cost);     printf("The spherical room costs \$%.2f to wallpaper.", DIAMETER * cost * PI);     return 0; }```
• 01-27-2005
Rouss
Please us code tags when you post code. Thanks.
This probably isn't the best answer, but if you don't want it to round, then why don't you just subtract .005 from the value. That way if it would have rounded, now it won't.
• 01-27-2005
mblazar
Thanks, I guess that works, Im sorry for not using the code tags. Im just wondering if there isnt another way of going about this?
• 01-27-2005
Kleid-0
You could try my snip function!
Code:

```#include <stdio.h> #include <stdlib.h> #include <math.h> // Snip the double value of where you want it to be snipped double snip (double d,  // The decimal value we want to snip       int dp)    // The decimal places of where to snip {   // Bring the decimal number up to an integer of where we want to cut   // And snip it   d = floor (d * pow (10, dp));   // Bring the integer back to a decimal by moving the decimal place back to the left   d = d / pow (10, dp);   // Successfully return the snipped version of the decimal   return d; } int main (void) {   // n = The number we want to snip   // n2 = The result of the snipping   double n = 3.14823723;   double n2;   // Snip the large decimal   n2 = snip (n, 2);   // Print out the results   printf ("%f = ~%f\n", n, n2);   // Return successfully   return 0; }```
• 01-28-2005
Salem
I don't get it - it outputs just two decimal places here just fine
Code:

```What is the cost of wallpaper per square meter in dollars? 1 The spherical room costs \$19113.45 to wallpaper.```
What answer did you get?
What answer did you expect?

I'm assuming this is a test, because that isn't the formula for the surface area of a sphere.
• 01-28-2005
Scribbler
Quote:

I am trying to make it so that my program won't round the result when its printed to only two decimal places on the screen.
Code:

`printf("The spherical room costs \$%.2f to wallpaper.", DIAMETER * cost * PI);`
If you don't want it to round to 2 decimal places, then don't include the .2 specifier in the %f string manipulator. Change %.2f to %f.
• 01-28-2005
Krak
Silly boy.

The surface area of a sphere is 4(pi*rē).

If this were measured in mē, then you'd just multiply whatever that formula gave you, times the cost.