# How to get a value Before/After a decimal point in C ??

This is a discussion on How to get a value Before/After a decimal point in C ?? within the C Programming forums, part of the General Programming Boards category; Hi, I'm new to C, I would like to achieve the following. get two numbers after the decimal point 23.456 ...

1. ## How to get a value Before/After a decimal point in C ??

Hi,

I'm new to C, I would like to achieve the following.

get two numbers after the decimal point

23.456 -> 0.45
11.235224 -> 0.23

and get all the numbers before the decimal point

23.456 -> 23
11.23 -> 11
.42 -> 0

How can I do it? I know it might be easy to some, but for me I still don't feel comforatble with C.

2. 23.456 is a floating point number

23.456 -> 23
Thats simply an integer version of your floating point number.

.456 is what's left if you subtract your integer from your float. You might have to play around with your code to get it to work right if you don't fully understand how c handles integers and floats.

3. Here is my code, but it doesn't quit work.

Code:
``` int main()

{

float cost; //Define the cost as float
float paid; //Define payments as float
float sum;

printf("Total Cost: ");
scanf ("%f", &cost);
printf("Paid: ");
scanf("%f", &paid ) ;
sum = paid - cost; //Subtract payments from the cost

printf ("\n%.d dollars", sum);

}```

4. You can use the trunc function in math.h to get the integer portion, then subtract that from the float to get the decimal portion. Depending on your setup, you may need to link the math library separately.

5. Code:
``` int main()
float sum;
...
printf ("\n%.d dollars", sum);```
Read up on how to use printf. You don't get to mix and match format specifies all willy-nilly. %d is for printing an integer (meaning sum is of type int, not "I want to drop the fractional part").

6. I'm trying to make a program that calculates the change , like below.

Total Cost: 2.23
Paid: 3.00

Results: \$.77

3 quarters
0 dimes
0 nickels
2 pennies

Let's say now we entered the following .

Total Cost: 3.25
Paid: 10.00

Result: \$6.75

27 quarters (I don't want it all to be in quarters, I need dollars too.)
0 dimes
0 nickels
0 pennies

so I was trying to take the value before the decimal point and count it as dollars and what's after I will count it in quarters, dimes or whatever as needed.

7. Here is my code

Code:
```#include <stdio.h>
#include <stdlib.h>

const int QUARTER = 25;
const int DIME = 10;
const int NICKEL = 5;
const int DOLLAR = 100;

int main()
{

float cost; //Define the cost as float
float paid; //Define payments as float
char ch;
float sum;
int sum2;
int amount, dollars, quarters, dimes, nickels, pennies;

printf("Total Cost: ");
scanf ("%f", &cost);
printf("Paid: ");
scanf("%f", &paid ) ;
sum = paid - cost; //Subtract payments from the cost

//printf ("\n%.f dollars", sum);
printf ("\n\n-------------");
printf ("\n%.2f Result", sum);
printf ("\n-------------");

amount = sum * 100;

quarters = amount / QUARTER;
amount = amount % QUARTER;
dimes = amount / DIME;
amount = amount % DIME;
nickels = amount / NICKEL;
pennies = amount % NICKEL ;

printf("\n\n%d quarters\n%d dimes\n%d nickels\n%d pennies\n",
quarters, dimes, nickels, pennies);

printf("\n\nContinue? (y/n)");
while(kbhit()) getch(); /* clear the input buffer */
do ch = getch(); while(ch!='y' && ch!='n');
while(ch=='y');
return 0;

}```

8. Originally Posted by mike65535
23.456 is a floating point number

Thats simply an integer version of your floating point number.

.456 is what's left if you subtract your integer from your float. You might have to play around with your code to get it to work right if you don't fully understand how c handles integers and floats.

I found it thanks, here is my code for those who will need it in the future.

Code:
```int main()

{

float cost; //Define the cost as float
float paid; //Define payments as float
int sum;
float sum2;

printf("Total Cost: ");
scanf ("%f", &cost);
printf("Paid: ");
scanf("%f", &paid ) ;
sum = paid - cost; //Subtract payments from the cost
printf ("\n%d dollars", sum);
sum2 = (paid - cost ) - sum;
printf ("\n%.2f Remainder", sum2);
//sum2 = sum - (paid - cost);
//printf ("\n%d dollars", sum);

return (0);
}```

Popular pages Recent additions