Pelles C forum

C language => Beginner questions => Topic started by: vedro-compota on May 02, 2012, 12:20:40 PM

Title: double (/float) type character by character
Post by: vedro-compota on May 02, 2012, 12:20:40 PM
hi there)
my brothers-programmers) please tell me - how to get double type character by character - i need make custom realization of fprintf - so it's necessary for me to show double type numder on the screen.

(For integer type this problem could be solved by subtracting 32 - and after that putchar() can be used.)

big thanks in advance)
Title: Re: double (/float) type character by character
Post by: CommonTater on May 02, 2012, 04:08:52 PM
Use sprintf(string,"%f",variable)  to print it to a string then you can do what you want with it.

See the Pelles C help file for more details...
Title: Re: double (/float) type character by character
Post by: vedro-compota on May 02, 2012, 08:31:02 PM
I can use only language kernel - e.g. only putchar for the screen (without libraries)))
Title: Re: double (/float) type character by character
Post by: Bitbeisser on May 02, 2012, 08:42:04 PM
I can use only language kernel - e.g. only putchar for the screen (without libraries)))
Maybe you should explain a bit more in detail what you are trying to do, so we understand what possible constraints you have (due to homework/course assignment?)...

Ralf
Title: Re: double (/float) type character by character
Post by: vedro-compota on May 02, 2012, 08:49:55 PM
yes - it's something like homework - i need to realize the fprintf()  - the base function for this  - as i understand is putchar() -
putchar() is enough for string type  =
Code: [Select]
%sand, as i think for int to
Code: [Select]
%i
Title: Re: double (/float) type character by character
Post by: Bitbeisser on May 02, 2012, 09:12:13 PM
yes - it's something like homework - i need to realize the fprintf()  - the base function for this  - as i understand is putchar() -
putchar() is enough for string type  =
Code: [Select]
%sand, as i think for int to
Code: [Select]
%i
Well, as a general rule (which I am sure you have been told already), we do not do any homework for you...  ;)

Beside that, doing it generally for floats isn't that much different that doing it for integers. You just have to to separate the whole number part from the fraction and run a loop for each of the digits to be output, with possible padding in front of the whole number part and filling with zeros possibly on the fraction part, up to a defined precision...

Ralf
Title: Re: double (/float) type character by character
Post by: vedro-compota on May 03, 2012, 07:11:15 AM
Quote
>>>>we do not do any homework for you...
i didn't asked you for this)
yes - I needed something  like this idea)thank you )
but this -
Quote
>>>>padding in front of the whole number part
I can't understand - could you give example (not code - just float number)
Title: Re: double (/float) type character by character
Post by: frankie on May 03, 2012, 09:07:31 AM
Take a look to http://www.koders.com/ (http://www.koders.com/) to have a look to printf or sprintf subroutines.
Title: Re: double (/float) type character by character
Post by: vedro-compota on May 03, 2012, 01:27:56 PM
thanks for advice, frankie)
there's a source code here (http://www.codeforge.com/article/67047), but the solution is quite massive...
Title: Re: double (/float) type character by character
Post by: frankie on May 03, 2012, 03:22:50 PM
I think that this http://www.koders.com/c/fid8684644B8325F0D91A103EA7750A9269E1F99856.aspx?s=ftoa#L651 (http://www.koders.com/c/fid8684644B8325F0D91A103EA7750A9269E1F99856.aspx?s=ftoa#L651) is enough for you (the only ftoa function).