Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
Photo

Convert string to double, int, long


  • Please log in to reply

#1
Qodeus

Qodeus

    New Member

  • Member
  • Pip
  • 5 posts
Hello! I need to convert a string type to double and int.

Please help. Thanks in advance!
  • 0

Advertisements


#2
destin

destin

    Member

  • Member
  • PipPip
  • 53 posts
What language?
  • 0

#3
Qodeus

Qodeus

    New Member

  • Topic Starter
  • Member
  • Pip
  • 5 posts
sorry i forgot - c++
  • 0

#4
Hai Mac

Hai Mac

    Member

  • Member
  • PipPipPip
  • 260 posts
try ((double))your_string;
  • 0

#5
Qodeus

Qodeus

    New Member

  • Topic Starter
  • Member
  • Pip
  • 5 posts
no way man. you can't convert string to double like that. :tazz: :)
  • 0

#6
Swandog46

Swandog46

    Malware Expert

  • Member
  • PipPipPipPip
  • 1,026 posts
  • MVP
Do you just mean a string of chars like:

char *string = "this is a string";

?

If so, use atoi() to convert it to an int, and then cast the int to a double.
  • 0

#7
destin

destin

    Member

  • Member
  • PipPip
  • 53 posts

Do you just mean a string of chars like:

char *string = "this is a string";

?

If so, use atoi() to convert it to an int, and then cast the int to a double.

The atoi(const char *) (ascii to integer) part was right, but if your string is "54.3", you will get 54 as the conversion, then casting to a double you will get 54.0. You need to use atof(const char *) (ascii to float) to convert to a float / double.

Edited by destin, 19 February 2006 - 07:23 PM.

  • 0

#8
Swandog46

Swandog46

    Malware Expert

  • Member
  • PipPipPipPip
  • 1,026 posts
  • MVP
Good point. I've never had to actually do this before in real programming. :tazz:
  • 0

#9
baroon

baroon

    Member

  • Member
  • PipPip
  • 41 posts
Why don't you try to make your know function to convert string containing double data. The logic is simple.

Let you have a string - "45.03". Now break your entire string to characters noting the position from decimal character. Write a code that multiply the character to 10 after taking exponent, acc. to positional value of character.

In "45.03 ", character "5" lies at position 0 from "." and character "4" lies at position 1 from ".". Now do multiply the numeric value of chracter to 10 after exponenting it via positional value of character, i.e, 5 * 10^0 , 4*10^1, 0*10^-1, 3*10^-2 and now add all the values you get from these operation for result.

I think you might be able to understand this logic.
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP