Please help. Thanks in advance!
Convert string to double, int, long
Started by
Qodeus
, Feb 18 2006 06:38 AM
#1
Posted 18 February 2006 - 06:38 AM
Please help. Thanks in advance!
#2
Posted 18 February 2006 - 08:44 AM
What language?
#3
Posted 18 February 2006 - 01:39 PM
sorry i forgot - c++
#4
Posted 19 February 2006 - 10:55 AM
try ((double))your_string;
#5
Posted 19 February 2006 - 01:00 PM
no way man. you can't convert string to double like that.
#6
Posted 19 February 2006 - 02:06 PM
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.
char *string = "this is a string";
?
If so, use atoi() to convert it to an int, and then cast the int to a double.
#7
Posted 19 February 2006 - 07:21 PM
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.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.
Edited by destin, 19 February 2006 - 07:23 PM.
#8
Posted 19 February 2006 - 08:23 PM
Good point. I've never had to actually do this before in real programming.
#9
Posted 09 March 2006 - 11:11 AM
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.
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.
Similar Topics
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users