Re: printf

From: Mike Wahler (mkwahler_at_mkwahler.net)
Date: 03/15/04


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



Relevant Pages

  • Trying to pad a 40 digit number with zeros with no luck
    ... I found printf and typeset -Z but have had no luck getting them to ... I am pulling a 10 character Hex value from a database, ... the cases where the beginning of the Hex is zeros. ...
    (comp.unix.shell)
  • Re: Trying to pad a 40 digit number with zeros with no luck
    ... I found printf and typeset -Z but have had no luck getting them to ... I am pulling a 10 character Hex value from a database, ... the cases where the beginning of the Hex is zeros. ...
    (comp.unix.shell)
  • Re: Converting Floats to Strings yields erratic results
    ... compare the results to printf(), ... I get an impossibly clean result of 1 followed by loads of zeros, ... after the last significant digit of the output string. ... usually seen real implementations do. ...
    (comp.lang.c)
  • printf
    ... I know the error is in printf. ... Either too many zeros or the decimal must be wrong. ... object files for financial analysis without GUI graphics. ...
    (comp.lang.c)
  • Re: Trying to pad a 40 digit number with zeros with no luck
    ... I am working on a script in SCO using the Korn shell. ... I found printf and typeset -Z but have had no luck getting them to ... Then I have to convert it back to Hex again and import it back into ... the cases where the beginning of the Hex is zeros. ...
    (comp.unix.shell)