Re: Problem with printf formats



Guenther Sohler wrote:
>
I have following code:

int main(void)
{
	printf("%.3lf\n",-2158470*0.001);
}

it prints

-2158.470

How can i disable '0' at the end, if they are not needed ?

Is there a possibility just to write %lf and just print as many
digits as needed to represent the number ?

You are asking the wrong question. The trailing zero is needed to represent the number. Otherwise how could you distinguish it from:


    printf("%.2f\n", -2158471 * 0.001)

(always assuming that -2158471 does not create an overflow)

Your question, I believe, is "how to suppress trailing zeroes". A little thought and an intermediary buffer should give you a suitable answer for that.

--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
.



Relevant Pages

  • Re: Preprocessor limitation workarounds
    ... #ifndef RADIX ... typedef unsigned short int digit_t; ... # error "Unable to represent the product of two digits" ...
    (comp.lang.c)
  • Re: binary number
    ... Integer literals ... An integer literal is a sequence of digits that has no period ... specifies its base and a suffix that specifies its type. ... these types in which its value can be represented: int, ...
    (comp.lang.cpp)
  • Re: System.Math.Round bug (repost)
    ... Round(double value, int digits) ... It always seems to use the Round(decimal value, int decimals) version, no ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: help me
    ... to exponential...if i use int then its not accepting more than 4 ... I think you've misunderstood your assignment. ... If you want the whole entry (17 digits) into *one* variable, ... This means that it's sufficient with an array of int's (even with bytes, ...
    (comp.lang.java.help)
  • Re: algorithm for brute force an variable lenght array
    ... (chromossomeint will range from 0 to 4) ... In an odometer, there are a series of wheels that count up from 0 ... digits ... OVERFLOW if the odometer overflows, ...
    (comp.lang.c)