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

Convert string to double, int, long

#1 Qodeus Posted 18 February 2006 - 06:38 AM

Qodeus

New Member

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

Please help. Thanks in advance!
• 0

destin

Member

• Member
• 53 posts
What language?
• 0

#3 Qodeus Posted 18 February 2006 - 01:39 PM

Qodeus

New Member

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

#4 Hai Mac Posted 19 February 2006 - 10:55 AM

Hai Mac

Member

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

#5 Qodeus Posted 19 February 2006 - 01:00 PM

Qodeus

New Member

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

#6 Swandog46 Posted 19 February 2006 - 02:06 PM

Swandog46

Malware Expert

• Member
• 1,026 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.
• 0

#7 destin Posted 19 February 2006 - 07:21 PM

destin

Member

• Member
• 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 Posted 19 February 2006 - 08:23 PM

Swandog46

Malware Expert

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

#9 baroon Posted 09 March 2006 - 11:11 AM

baroon

Member

• Member
• 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