Thread: String to int using strtok and strtol

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    11

    String to int using strtok and strtol

    I just had a quick question. I need to put a number from a string and convert it to int/long/or double. the number is a price so it has a decimal and 2 numbers after, such as 48.78. When i try to convert it it only converts the numbers before the decimal. How would I convert these into my int? thanks

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    For answers regarding code, post the code in question.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    An int won't hold the decimals, because it is meant to store stricly integers.

    What you'd want is strtod, which will convert your string to a double, and then you store that in a double.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading in data from Text file
    By fortune2k in forum C Programming
    Replies: 214
    Last Post: 04-10-2008, 11:12 AM
  2. converting string to integer, for further use
    By shoobsie in forum C Programming
    Replies: 2
    Last Post: 07-01-2005, 03:12 AM