Re: how to get 0.000001 with printf?




"William Hughes" <wpihughes@xxxxxxxxxxx> wrote in message
news:1131114153.485468.294050@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
>
> pemo wrote:
>> "Mandar Mitra" <mandar.mitra@xxxxxxxxx> wrote in message
>> news:1130938589.337691.51630@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
>> >> The format requested in this thread is analogous to the "general
>> >> number" format in programs such as Microsoft Excel.
>> >
>> > Aah, that explains it.
>> >
>> > Here's why I asked the original question: I have been given a program
>> > (written in a VC++ environment) that I have to clean up and
>> > re-implement.
>> > I was hoping to verify the correctness of my paraphrase by running a
>> > "diff" on the various intermediate files generated by the 2 versions.
>> > Unfortunately, the numbers in the original intermediate files are in
>> > "microsoft" format, so diff was throwing up a lot of output.
>> >
>> > Perhaps I should have explained this context in the original post. It
>> > would have saved everyone a lot of trouble.
>> >
>> > Anyway, thanks for the illuminating discussion.
>>
>> Here's my stab at it ...
>>
>> #include <stdio.h>
>> #include <string.h>
>>
>> char * removeTralingZeros(char * buffer)
>> {
>> int i;
>>
>> char * noughtPointer;
>>
>> for(i = 0, noughtPointer = NULL; buffer[i] != '\0'; ++i)
>> {
>> if(buffer[i] == '0' && noughtPointer == NULL)
>> {
>> noughtPointer = &buffer[i];
>> }
>>
>> else
>>
>> if(buffer[i] != '0')
>> {
>> noughtPointer = NULL;
>> }
>> }
>>
>> if(noughtPointer != NULL)
>> {
>> *noughtPointer = '\0';
>> }
>>
>> return buffer;
>> }
>>
>>
>> int main(void)
>> {
>> char buffer[100];
>>
>> // User input?
>> //
>> strcpy(&buffer[0], "3.14000100");
>>
>> puts(removeTralingZeros(&buffer[0]));
>>
>> getchar();
>>
>> return 0;
>> }
>
> This only answers one part of the question (and that the easy part).
>
> Assume that the floating point number x, represents
> the rational 2.19994019993
>
> Should this print out as
>
> 2.2
> 2.1999
> 2.19994
> 2.1999402
> 2.1999401999
>
> or
>
> 2.19994019993
>
> This question cannot be answered by merely looking at x and deciding to
> strip trailing zeros. You have to decide which decimal place to
> round to.


I can't see the OP, but the extract I did see was

Mandar Mitra wrote:
> Hello,
>
> I'd like to print floating point numbers without trailing zeros.

So, that's what I answered.


.



Relevant Pages