# Mathematica covert string to number problem - help

Printable View

• Jan 7th 2010, 09:14 PM
muiz
Mathematica covert string to number problem - help
Dear all, now I'm study mathematica, and face a problem like this:

I want to covert string "1107.302" to a double number and then do some calculation.

Code:

```      a = "1107.302 ";       Print[a];                >> Output is 1107.302       b=ToExpression[a];          Print[b];                >> Output is 1107.3       c = b * 2;       .......```
But the ToExpression function lost the number "02", then what can I do? Any suggestions?

Thanks in advance!
• Jan 7th 2010, 09:21 PM
CaptainBlack
Quote:

Originally Posted by muiz
Dear all, now I'm study mathematica, and face a problem like this:

I want to covert string "1107.302" to a double number and then do some calculation.

Code:

```      a = "1107.302 ";       Print[a];                >> Output is 1107.302       b=ToExpression[a];          Print[b];                >> Output is 1107.3       c = b * 2;       .......```
But the ToExpression function lost the number "02", then what can I do? Any suggestions?

Thanks in advance!

It is always a good idea to tell us what package/language you are using.

Check default format for the display of numerical data (instead of Print[b] try Print[(b-1107)*1000] )

CB
• Jan 7th 2010, 09:35 PM
muiz
Thanks very much, CB!

I just new a noteBook and type the following codes:

Code:

```      a = "1107.302 ";       b = ToExpression[a];       Print[b - 1107];        >> Out put:  0.302 , this is correct       Print[b];                  >> Out put:  1107.3```
Then I want to export the b's value to a temp file, I hope the value should be 1107.302, but actually, it's 1107.3.

What can I do then?
• Jan 7th 2010, 10:13 PM
CaptainBlack
Quote:

Originally Posted by muiz
Thanks very much, CB!

I just new a noteBook and type the following codes:

Code:

```      a = "1107.302 ";       b = ToExpression[a];       Print[b - 1107];        >> Out put:  0.302 , this is correct       Print[b];                  >> Out put:  1107.3```
Then I want to export the b's value to a temp file, I hope the value should be 1107.302, but actually, it's 1107.3.

What can I do then?

The value is correct, change the format to something that will show a field width of 10 digits with three after the decimal point. I can't tell you how to do that because I don't run Mathematica, but it will tell you somewhere in the help system.

CB
• Jan 7th 2010, 11:54 PM
muiz
Thanks, CB. I'm checking in help system.