Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Converting from string to real

124 views
Skip to first unread message

fcaleyo

unread,
Jun 6, 2012, 4:51:35 AM6/6/12
to
Hi guys, I've got a problem with ToExpression[].
It seems to round large real numbers off, e.g.
ToExpression["109266.75"] returns 109267., still a real, yet not exactly what the original string meant.
Any idea to solve this?
Many thanks
fcaleyo




Adriano Pascoletti

unread,
Jun 7, 2012, 5:19:43 AM6/7/12
to

Internally it is as you like:

In[4]:= InputForm[ToExpression["109266.75"]]
Out[4]//InputForm= 109266.75


Adriano Pascoletti

2012/6/6 fcaleyo <fca...@gmail.com>

Bill Rowe

unread,
Jun 7, 2012, 5:23:16 AM6/7/12
to
On 6/6/12 at 4:53 AM, fca...@gmail.com (fcaleyo) wrote:

>Hi guys, I've got a problem with ToExpression[]. It seems to round
>large real numbers off, e.g. ToExpression["109266.75"] returns
>109267., still a real, yet not exactly what the original string
>meant. Any idea to solve this? Many thanks fcaleyo

This isn't as you describe. By default Mathematica *displays*
real numbers rounded to six significant digits. But the value
used by Mathematica is not rounded in that manner. This can be
seen by the following:

In[3]:= a = ToExpression["109266.75"]

Out[3]= 109267.

In[4]:= RealDigits[a]

Out[4]= {{1,0,9,2,6,6,7,5,0,0,0,0,0,0,0,0},6}

You can set your preferences so that Mathematica displays either
more or less significant digits f you are so inclined.


Szabolcs Horvát

unread,
Jun 7, 2012, 5:24:18 AM6/7/12
to
On 2012.06.06. 10:51, fcaleyo wrote:
> Hi guys, I've got a problem with ToExpression[].
> It seems to round large real numbers off, e.g.
> ToExpression["109266.75"] returns 109267., still a real, yet not exactly what the original string meant.
> Any idea to solve this?

By default machine precision number are displayed only with six digits,
but the precision is still there. If you position the cursor on the
output cell and press space, you'll see this.

Also try FractionalPart@ToExpression["109266.75"]

So don't worry, only the display is truncated for convenience (in your
case inconvenience but most of the time it's better not to see all 15 or
so digits all at once)

--
Szabolcs Horvát
Visit Mathematica.SE: http://mathematica.stackexchange.com/

A Retey

unread,
Jun 7, 2012, 5:24:48 AM6/7/12
to
Am 06.06.2012 10:51, schrieb fcaleyo:
> Hi guys, I've got a problem with ToExpression[].
> It seems to round large real numbers off, e.g.
> ToExpression["109266.75"] returns 109267., still a real, yet not exactly what the original string meant.

it doesn't round, it just doesn't print more than 6 digits with the
default settings.

> Any idea to solve this?

You can use InputForm to see that it really knows all digits (and will
use them for any further calculation):

ToExpression["109266.75"]//InputForm

You could also change the "Displayed precision" from Edit -> Preferences
in the Apearance -> Numbers -> Formatting tab.

Other than that, there isn't anything to solve really :-)

hth,

albert


Bob Hanlon

unread,
Jun 7, 2012, 5:25:19 AM6/7/12
to
You are seeing the effect of the preference setting for the
PrintPrecision display option. All of the digits can be seen by
controlling the display (or changing the preference).

x = {ToExpression["109266.75"], Pi//N}

{109267.,3.14159}

x//InputForm

{109266.75, 3.141592653589793}

x//FullForm

List[109266.75`,3.141592653589793`]

NumberForm[x,8]

{109266.75,3.1415927}


Bob Hanlon


On Wed, Jun 6, 2012 at 4:53 AM, fcaleyo <fca...@gmail.com> wrote:
> Hi guys, I've got a problem with ToExpression[].
> It seems to round large real numbers off, e.g.
> ToExpression["109266.75"] returns 109267., still a real, yet not exactly what the original string meant.
> Any idea to solve this?
> Many thanks
> fcaleyo
>

Kevin J. McCann

unread,
Jun 7, 2012, 5:26:50 AM6/7/12
to
This is an output formatting issue, and I will admit that it is
annoying. Note:

ToExpression["109266.75"]-109266.75

gives zero. So, the answer is correct, it is just that Mathematica formats the
output by rounding it. I am not sure how to make it show the way you
want in an easy fashion; however, this works

NumberForm[ToExpression["109266.75"],10]

but this is a formatted output "for your eyes only", i.e. DON'T do
something like

b = NumberForm[ToExpression["109266.75"],10]

and then try to use b as a number.

Kevin

Peter Breitfeld

unread,
Jun 7, 2012, 5:27:21 AM6/7/12
to
This is only due to the fact, that Mathematica displays 6 Digits of precision.
If you use

ToExpression["109266.75"]//InputForm

you will see, that all digits are present.

--
_________________________________________________________________
Peter Breitfeld | Bad Saulgau, Germany | http://www.pBreitfeld.de

Nasser M. Abbasi

unread,
Jun 7, 2012, 5:16:08 AM6/7/12
to
You can change the PrintPrecision to larger value

--------------------------------------------------
SetOptions[$FrontEndSession,PrintPrecision->15]
ToExpression["109266.75"]

109266.75
------------------------------------------------

To make it permanent, search your advanced
preferences (Options Inspector) for PrintPrecision
and change it there from the default 6 to any other value.

--Nasser


George Woodrow III

unread,
Jun 7, 2012, 5:16:39 AM6/7/12
to
All the digits are there.

Use InputForm[] to see what the number really is. By default, Mathematica just shows 6 digits of a real number (when it can).

In[1]:= ToExpression["109266.75"]
Out[1]= 109267.

In[2]:= InputForm[%]
Out[2]//InputForm=
109266.75


george woodrow
0 new messages