Re: Fast conversion of a float to character string ( C language )

From: James Kanze (kanze_at_gabi-soft.fr)
Date: 09/11/04


Date: 11 Sep 2004 22:59:42 +0200

Paul Lutus <nospam@nosite.zzz> writes:

|> flair wrote:

|> / ...

|> > Thank you for your response.
|> > Actually, I have to display float numbers that always have one of
|> > the following formats:

|> > "8888.88"
|> > "888.88"
|> > "88.88"
|> > "8.88"
|> > "-8888.88"
|> > "-888.88"
|> > "-88.88"
|> > "-8.88"
|> > It looks simple.

|> Those are not formats per se, they represent properties of the data.
|> I mean, 8.88 looks different than 888.88, not because of a
|> formatting choice, but because it is a different number.

Always two digits after the decimal. And the values aren't that large,
either. The easiest way to handle that would be to multiply by 100,
convert to int, convert the int to a string, and then insert the
decimal.

|> > I have written my own function for the conversion. It improved the
|> > speed of my program but I am sure I can find a better algorithm.

|> I am willing to bet your algorithm cannot handle an entire class of
|> rounding problems that come up with conversion from binary to
|> decimal. If your algorithm is faster than sprintf(), it does it by
|> taking shortcuts that don't matter in 99% of cases.

|> Example. Show your algorithm's output with different numbers of
|> decimal places (or absolute magnitude) for a double variable loaded
|> with the results for 4.0/9.0 and 5.0/9.0 respectively.

|> On the other hand, if your program must format currency values and
|> that is its only purpose, you may be better off using a method that
|> converts the doubles to a properly rounded long that is aligned with
|> pennies (e.g. it resesents an integer number of pennies). In this
|> arrangement, after the conversion, formatting and printing the
|> numbers in decimal form is deterministic and reliable.

If his program must deal with currency values, there is a good chance
that he has to get some sort of package which does decimal arithmetic.

-- 
James Kanze
Conseils en informatique orientée objet/
                  Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France +33 (0)1 30 23 00 34


Relevant Pages

  • Re: RfD: IEEE-FP
    ... > You don't have to define new words for all the IEEE 754 operations. ... >> IEEE 754-2008 defines three basic binary fp formats, binary32, ... See IEEE 5.4.2, "Conversion operations ... >> binary32 or binary64 interchange formats, ...
    (comp.lang.forth)
  • Re: Outlook 2003: RTF-to-HTML Table conversion
    ... I think your best solution is to teach your users who have problems with certain correspondents how to configure their message formats. ... expierenced with HTML and the Outlook Editor). ... I assumed Exchange is doing the conversion because the message's HTML source ...
    (microsoft.public.exchange.clients)
  • Re: Converting all old Office Files to 2007 format
    ... I understand MS does not want to support old formats forever. ... On the other hand NOT supporting old formats and forcing customers ... MS does have a bulk conversion tool for moving to Office 2007 as part of the Office Migration Planning Manager, ...
    (microsoft.public.word.docmanagement)
  • Re: Floating Point and C casts? (was: Re: typecast internals)
    ... You are mixing a discussion of integer and floating point storage ... What particular data conversion problem are you really ... formats), but these formats are not typically implemented off the VAX ... IEEE floating point conversions are detailed in the compiler manuals ...
    (comp.os.vms)
  • Re: Converting VMS Formats to IEEE equivalents
    ... "Dale Pennington" wrote in message ... > I am working a project on an IBM AIX system that is getting data from a ... The VMS system is providing the data in the F & G Float formats, ... The conversion can easily be done on the VMS system using the library call; ...
    (comp.os.vms)