exact conversion of floating point to string

This is a discussion on exact conversion of floating point to string within the C Programming forums, part of the General Programming Boards category; Hi all, first time posting here. i have searched through all function like sprintf, ftoa, ecvt, fcvt, gcvt. but i ...

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    8

    Lightbulb exact conversion of floating point to string

    Hi all,
    first time posting here.

    i have searched through all function like sprintf, ftoa, ecvt, fcvt, gcvt.
    but i cannot find a function that can convert the exact content of a floating point to string.

    for example, 3.141e2 declared in double, i need to convert to string format, and the resultant must be the same content.

    can anyone help me out with this problem.

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    You mean you want it to print out:

    '3.141e2'

    ..or would it be acceptable to have:

    '314.1'

    ?

    If the former, you could try something like

    Code:
        double some_var = 3.141e2;
    
        printf("\n%.3e\n", some_var);
    If the latter,

    Code:
        double some_var = 3.141e2;
    
        printf("\n%g\n", some_var);
    Last edited by kermit; 07-21-2005 at 09:47 PM.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    sprintf() should work. Or printf(), like kermit said.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Jul 2005
    Posts
    8

    Smile

    Quote Originally Posted by kermit
    You mean you want it to print out:

    '3.141e2'

    ..or would it be acceptable to have:

    '314.1'

    ?

    If the former, you could try something like

    Code:
        double some_var = 3.141e2;
    
        printf("\n%.3e\n", some_var);
    If the latter,

    Code:
        double some_var = 3.141e2;
    
        printf("\n%g\n", some_var);
    thanks for the reply,

    however i am getting the value of type double from another function. i will not be able to know what the content is . so it may be 3.14 or 3.14e+1.

    i need to convert this value of type double to type string for some other usage. and i need the content to be the exact content . meaning what i get will be convert to the same thing but in different type.

    i tried sprintf, but sprintf need to declare %f,%e. and i do not know what content it is .

    i tried ecvt,fcvt,gcvt. all modified my data, do you all know what function i can use so that the content is not modified but converted to type string

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,824
    Floating point numbers are mostly inexact approximations.
    Therefore string representations of them will also be inexact, most of the time.
    http://docs.sun.com/source/806-3568/ncg_goldberg.html
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    sprintf() will not modify your data. Try %g as kermit suggested.

    (Or try %02g to limit your output to two decimal points.)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Jul 2005
    Posts
    8
    Thanks to all that have helped me.

    sprintf works, thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 05:05 AM
  2. Testing Floating Point Return Values
    By jason_m in forum C Programming
    Replies: 5
    Last Post: 08-15-2008, 02:37 PM
  3. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  4. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  5. floating point to string
    By Sathyabodh in forum C Programming
    Replies: 2
    Last Post: 08-13-2003, 03:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21