converting string to float

This is a discussion on converting string to float within the C Programming forums, part of the General Programming Boards category; Hi, I'm having a problem converting a 9 character string(which contains only digits ) to a float. I've tried using ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    1

    Question converting string to float

    Hi,

    I'm having a problem converting a 9 character string(which contains only digits ) to a float.

    I've tried using atof and typecasting to float, but end up with a number which has 7 correct digits and 2 incorrect ones.

    I've also tried converting string to long int, then dividing the long int by 100 and typecasting the result as float.

    Neither approach seems to work, are there any other ways I may try?

    Thanks in advance,

    TWHUBS

  2. #2
    Unregistered
    Guest
    > I'm having a problem converting a 9 character string
    floats are only accurate to 6 decimal digits, so you're always going to lose the last 2 or 3 digits no matter what you do.

    > are there any other ways I may try?
    Use a double instread of a float

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. Moving Average Question
    By GCNDoug in forum C Programming
    Replies: 4
    Last Post: 04-23-2007, 11:05 PM
  4. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM

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