Re: printf
From: Mike Wahler (mkwahler_at_mkwahler.net)
Date: 03/15/04
- Next message: Monika Levinsky: "Expert preprocessor question"
- Previous message: CBFalconer: "Re: pointer problems"
- In reply to: Bill Cunningham: "printf"
- Next in thread: Martin Ambuhl: "Re: printf"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Mon, 15 Mar 2004 02:39:08 GMT
"Bill Cunningham" <nospam@nspam.net> wrote in message
news:4054f994_3@127.0.0.1...
> I want printf to give me a whole number and fractional part from a
division
> operation. Here's the source I have now.
>
> double relstr(double x,double y)
> {double n;
> n=x/y;
> printf("%.000f",n);
> return n;}
>
> I know the error is in printf. The result I always get is 1. I tried
> relstr(25,5) and relstr(5,25). 25/5 is 5 not 1. The error is somewhere in
> the decimal and zeros. Either too many zeros or the decimal must be wrong.
I
> guess I'm just feeling lazy today and would like someone to help critique
my
> code. I'm in the process of writing a static library called libstk.a to
hold
> object files for financial analysis without GUI graphics.
>
> Bill
Look up what those zeros after the decimal point
in your '%' specification mean.
#include <stdio.h>
double relstr(double x, double y)
{
double n = x / y;
printf("%f", n);
return n;
}
int main()
{
relstr(25, 5);
putchar('\n');
relstr(5, 25);
putchar('\n');
return 0;
}
Output:
5.000000
0.200000
If this isn't what you meant, try explaining more
clearly.
-Mike
- Next message: Monika Levinsky: "Expert preprocessor question"
- Previous message: CBFalconer: "Re: pointer problems"
- In reply to: Bill Cunningham: "printf"
- Next in thread: Martin Ambuhl: "Re: printf"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|