# DON'T USE .ToInteger or ToInt64!

**URL:** <https://forum.xojo.com/t/dont-use-tointeger-or-toint64/65067>\
**Category:** General\
**Created:** [August 16, 2021, 7:55pm UTC](https://forum.xojo.com/t/dont-use-tointeger-or-toint64/65067 "2021-08-16T19:55:35Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Markus\_Winter](https://forum.xojo.com/user_avatar/forum.xojo.com/markus_winter/32/144_2.png) [@Markus\_Winter](https://forum.xojo.com/u/Markus_Winter)\
**Post date:** [August 16, 2021, 7:55pm UTC](https://forum.xojo.com/t/dont-use-tointeger-or-toint64/65067/1 "2021-08-16T19:55:35Z")

</div>

From Norman’s blog (worth keeping an eye on!):

So a quick test in 2021r2.1 with

```auto
Dim s As String = "3.0e+2"

Dim i1 As Int32 = Val(s)
Dim i2 As Int32 = s.Val
Dim i3 As Int32 = s.ToInteger
Break

```

shows that indeed i1 = 300, i2 = 300, and **i3 is 3 not 300** like I would have expected. I changed my use of ToInteger to Val and things work as expected.

[http://feedback.xojo.com/case/65567](http://feedback.xojo.com/case/65567)

Incredible. 🤦‍♂️

Does Xojo not use unit tests? How is it possible that such a bug slips through?

Time for **another** emergency fix release …

---

<div class="post-metadata">

**Author:** ![Douglas\_Handy](https://forum.xojo.com/letter_avatar_proxy/v4/letter/d/d2c977/32.png) [@Douglas\_Handy](https://forum.xojo.com/u/Douglas_Handy)\
**Post date:** [August 16, 2021, 8:00pm UTC](https://forum.xojo.com/t/dont-use-tointeger-or-toint64/65067/2 "2021-08-16T20:00:38Z")

</div>

Actually, I am more surprised i1 (the Val(s) method) comes up with 300. I don’t need scientific notation often, but my general view of Val() on a string it is will quit at the first character that is not a digit, +/- sign, or decimal separator (period in the USA).

So I am more surprised that i1 is 300; I would have guessed it said 3.

Not that is will impact any of my programs either way…

---

<div class="post-metadata">

**Author:** ![DerkJ](https://forum.xojo.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@DerkJ](https://forum.xojo.com/u/DerkJ)\
**Post date:** [August 16, 2021, 8:02pm UTC](https://forum.xojo.com/t/dont-use-tointeger-or-toint64/65067/3 "2021-08-16T20:02:11Z")

</div>

This is expected behaviour since it converts to an integer. It’s also explained in the docs:

[https://documentation.xojo.com/api/data\_types/string.html.ToInteger](https://documentation.xojo.com/api/data_types/string.html.ToInteger)

Aleast they mention to use val where possible.

It’s more likely unexpected behaviour to get the scientific notification for such short value…

---

<div class="post-metadata">

**Author:** ![Markus\_Winter](https://forum.xojo.com/user_avatar/forum.xojo.com/markus_winter/32/144_2.png) [@Markus\_Winter](https://forum.xojo.com/u/Markus_Winter)\
**Post date:** [August 16, 2021, 8:05pm UTC](https://forum.xojo.com/t/dont-use-tointeger-or-toint64/65067/4 "2021-08-16T20:05:14Z")

</div>

Sorry, Scientific Notation is a valid number, and Val works just fine with it.

> # String.ToInteger
> 
> ### From Xojo Documentation
> 
> Method
> 
> Returns the integer equivalent of the source [string](https://documentation.xojo.com/api/data_types/string.html). **This function is the same as the** [Val](https://documentation.xojo.com/api/text/val.html) **function** but is international-savvy.

So it is clearly a bug.

---

<div class="post-metadata">

**Author:** ![DerkJ](https://forum.xojo.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@DerkJ](https://forum.xojo.com/u/DerkJ)\
**Post date:** [August 16, 2021, 8:07pm UTC](https://forum.xojo.com/t/dont-use-tointeger-or-toint64/65067/5 "2021-08-16T20:07:19Z")

</div>

It’s clearly in the docs:

> Numbers are converted only if they are found at the beginning of the string. Any numbers that follow a non-numeric value are ignored.

---

<div class="post-metadata">

**Author:** ![Markus\_Winter](https://forum.xojo.com/user_avatar/forum.xojo.com/markus_winter/32/144_2.png) [@Markus\_Winter](https://forum.xojo.com/u/Markus_Winter)\
**Post date:** [August 16, 2021, 8:10pm UTC](https://forum.xojo.com/t/dont-use-tointeger-or-toint64/65067/6 "2021-08-16T20:10:08Z")

</div>

It is not that difficult to understand that Scientific Notation **IS** a numeric value. Please don’t apply primary school maths concepts of numbers to modern programming, that’s ridiculous. Numbers are more than 0 to 9.

And in case you didn’t even read the documentation let me spell it out for you:

- toInteger is stated to work the SAME as Val (I even had bolded it in the original post).
- Val does it correctly
- toInteger does NOT do it correctly
- So toInteger is NOT working the same as Val

THAT IS A BUG and leads to WRONG RESULTS

---

<div class="post-metadata">

**Author:** ![Markus\_Winter](https://forum.xojo.com/user_avatar/forum.xojo.com/markus_winter/32/144_2.png) [@Markus\_Winter](https://forum.xojo.com/u/Markus_Winter)\
**Post date:** [August 16, 2021, 8:20pm UTC](https://forum.xojo.com/t/dont-use-tointeger-or-toint64/65067/7 "2021-08-16T20:20:12Z")

</div>

And just try:

```auto
Var b As Boolean
b = IsNumeric("3e2") // <- b is TRUE
Break

```

QED

---

<div class="post-metadata">

**Author:** ![Douglas\_Handy](https://forum.xojo.com/letter_avatar_proxy/v4/letter/d/d2c977/32.png) [@Douglas\_Handy](https://forum.xojo.com/u/Douglas_Handy)\
**Post date:** [August 16, 2021, 8:33pm UTC](https://forum.xojo.com/t/dont-use-tointeger-or-toint64/65067/8 "2021-08-16T20:33:11Z")

</div>

> [@Markus\_Winter](#):
>
> It is not that difficult to understand that Scientific Notation **IS** a numeric value. Please don’t apply primary school maths concepts of numbers to modern programming, that’s ridiculous. Numbers are more than 0 to 9.

In general though, I shy away from Val() too because it also has never correctly handled separators. Have you ever tried Val(1,000.00) ?

The string 1,000.00 is a primary school math concept, and it fails too.

I’ll agree the docs say .ToInteger should be the same as Val() and it evidently is not. Will that lead to wrong results? Not in my code. But yes, I will concur it is a bug.

---

<div class="post-metadata">

**Author:** ![GarryPettet](https://forum.xojo.com/user_avatar/forum.xojo.com/garrypettet/32/31_2.png) [@GarryPettet](https://forum.xojo.com/u/GarryPettet)\
**Post date:** [August 16, 2021, 8:33pm UTC](https://forum.xojo.com/t/dont-use-tointeger-or-toint64/65067/9 "2021-08-16T20:33:12Z")

</div>

Yep. This does seem to be a bug. It certainly is not equivalent to `Val`.

I think it should be corrected rather than the the docs being altered.

---

<div class="post-metadata">

**Author:** ![anon20074439](https://forum.xojo.com/letter_avatar_proxy/v4/letter/a/ac8455/32.png) [@anon20074439](https://forum.xojo.com/u/anon20074439)\
**Post date:** [August 16, 2021, 8:39pm UTC](https://forum.xojo.com/t/dont-use-tointeger-or-toint64/65067/10 "2021-08-16T20:39:22Z")

</div>

Nice little bug.

I also don’t understand this at the top of String.ToInteger

`This function is the same as the Val function but is international-savvy.`

Neither 3,000.12 nor 3.000,12 come out with 3000 for me is that what international-savvy is meant to be or am I missing something there?

Then there’s `For localized number formatting, use the ToInteger function instead.`

I think that should be `Integer.ToString` not `ToInteger` ?, as we’re already on the ToInteger page #confusing

---

<div class="post-metadata">

**Author:** ![Markus\_Winter](https://forum.xojo.com/user_avatar/forum.xojo.com/markus_winter/32/144_2.png) [@Markus\_Winter](https://forum.xojo.com/u/Markus_Winter)\
**Post date:** [August 16, 2021, 8:39pm UTC](https://forum.xojo.com/t/dont-use-tointeger-or-toint64/65067/11 "2021-08-16T20:39:36Z")

</div>

> [@Douglas\_Handy](#):
>
> The string 1,000.00 is a primary school math concept, and it fails too.

That **is** actually mentioned in the Val documentation:

> For localized number formatting, use the [CDbl](https://documentation.xojo.com/api/deprecated/cdbl.html) function instead. Generally, you will use **Val** for converting internal data and use [CDbl](https://documentation.xojo.com/api/deprecated/cdbl.html) for converting data for input and output of user data.

---

<div class="post-metadata">

**Author:** ![AlbertoD](https://forum.xojo.com/letter_avatar_proxy/v4/letter/a/dbc845/32.png) [@AlbertoD](https://forum.xojo.com/u/AlbertoD)\
**Post date:** [August 16, 2021, 8:42pm UTC](https://forum.xojo.com/t/dont-use-tointeger-or-toint64/65067/12 "2021-08-16T20:42:05Z")

</div>

Same with .ToInt64  
If you see the note:

> The **ToInt64** function stops reading the string at the first character it doesn’t recognize as part of a number. All other characters are automatically stripped.

it looks like ‘e’ is not recognized as part of a number.

---

<div class="post-metadata">

**Author:** ![Markus\_Winter](https://forum.xojo.com/user_avatar/forum.xojo.com/markus_winter/32/144_2.png) [@Markus\_Winter](https://forum.xojo.com/u/Markus_Winter)\
**Post date:** [August 16, 2021, 8:45pm UTC](https://forum.xojo.com/t/dont-use-tointeger-or-toint64/65067/13 "2021-08-16T20:45:26Z")

</div>

> [@AlbertoD](#):
>
> Same with .ToInt64

😱 🤦‍♂️

Time for a new “Xojo Maths is badly broken” thread …? 🤔

---

<div class="post-metadata">

**Author:** ![AlbertoD](https://forum.xojo.com/letter_avatar_proxy/v4/letter/a/dbc845/32.png) [@AlbertoD](https://forum.xojo.com/u/AlbertoD)\
**Post date:** [August 16, 2021, 8:46pm UTC](https://forum.xojo.com/t/dont-use-tointeger-or-toint64/65067/14 "2021-08-16T20:46:17Z")

</div>

> [@anon20074439](#):
>
> I think that should be `Integer.ToString` not `ToInteger` ?, as we’re already on the ToInteger page #confusing

There is Integer.FromString but that doesn’t work, not even the “1,234” sample code on that page:  
63596 - Integer.FromString macOS API 2 locale bug causing exception on formatted strings

Edit: Windows works, mac doesn’t work (mentioned in the report)

---

<div class="post-metadata">

**Author:** ![anon20074439](https://forum.xojo.com/letter_avatar_proxy/v4/letter/a/ac8455/32.png) [@anon20074439](https://forum.xojo.com/u/anon20074439)\
**Post date:** [August 16, 2021, 8:50pm UTC](https://forum.xojo.com/t/dont-use-tointeger-or-toint64/65067/15 "2021-08-16T20:50:23Z")

</div>

> [@AlbertoD](#):
>
> There is Integer.FromString but that doesn’t work, not even the “1,234” sample code on that page

Wow, I tried it in Windows and it worked ok, guess not on mac, oof, February, damn.

---

<div class="post-metadata">

**Author:** ![Markus\_Winter](https://forum.xojo.com/user_avatar/forum.xojo.com/markus_winter/32/144_2.png) [@Markus\_Winter](https://forum.xojo.com/u/Markus_Winter)\
**Post date:** [August 16, 2021, 8:56pm UTC](https://forum.xojo.com/t/dont-use-tointeger-or-toint64/65067/16 "2021-08-16T20:56:20Z")

</div>

> [@anon20074439](#):
>
> I tried it in Windows and it worked ok

Really? You mean this:

```auto
Var Locale As New Locale("en-US")
Var value As Integer
value = Integer.FromString("1,234", Locale) // value = 1234

```

On my Mac with Xojo 2021R2.1 and MacOS X Mojave:

**RunTimeException on the third line with “1,234 is not a valid integer.”**

And “someone” at Xojo thinks Xojo does not have a bug problem …

---

<div class="post-metadata">

**Author:** ![Douglas\_Handy](https://forum.xojo.com/letter_avatar_proxy/v4/letter/d/d2c977/32.png) [@Douglas\_Handy](https://forum.xojo.com/u/Douglas_Handy)\
**Post date:** [August 16, 2021, 8:57pm UTC](https://forum.xojo.com/t/dont-use-tointeger-or-toint64/65067/17 "2021-08-16T20:57:58Z")

</div>

> [@Markus\_Winter](#):
>
> That **is** actually mentioned in the Val documentation:

I realize that, and is one reason I tend to NOT use Val() for anything unless I am really sure the data will be converted as expected. Since I have not needed scientific notation in over 40 years of _my_ programming, it is not on my radar.

YMMV.

I’m not saying it should not be fixed; .ToInteger clearly does not act the same as Val() as the docs state. I just think Val() is also not as useful as some people assume it is. And if .ToInteger is fixed to operate the same as Val(), I’d avoid it too for most purposes.

---

<div class="post-metadata">

**Author:** ![anon20074439](https://forum.xojo.com/letter_avatar_proxy/v4/letter/a/ac8455/32.png) [@anon20074439](https://forum.xojo.com/u/anon20074439)\
**Post date:** [August 16, 2021, 8:59pm UTC](https://forum.xojo.com/t/dont-use-tointeger-or-toint64/65067/18 "2021-08-16T20:59:14Z")

</div>

> [@Markus\_Winter](#):
>
> On my Mac with Xojo 2021R2.1 and MacOS X Mojave:

> [@anon20074439](#):
>
> I tried it in Windows and it worked ok, **guess not on mac**

🙂

---

<div class="post-metadata">

**Author:** ![Markus\_Winter](https://forum.xojo.com/user_avatar/forum.xojo.com/markus_winter/32/144_2.png) [@Markus\_Winter](https://forum.xojo.com/u/Markus_Winter)\
**Post date:** [August 16, 2021, 9:46pm UTC](https://forum.xojo.com/t/dont-use-tointeger-or-toint64/65067/19 "2021-08-16T21:46:04Z")

</div>

> [@Douglas\_Handy](#):
>
> YMMV

It does. Scientific Notation is very common in - surprise - Science 😉

> [@Douglas\_Handy](#):
>
> but my general view of Val() on a string it is will quit at the first character that is not a digit, +/- sign, or decimal separator (period in the USA).

… and that view is clearly wrong. So time to adopt a better one. 🤓

---

<div class="post-metadata">

**Author:** ![Markus\_Winter](https://forum.xojo.com/user_avatar/forum.xojo.com/markus_winter/32/144_2.png) [@Markus\_Winter](https://forum.xojo.com/u/Markus_Winter)\
**Post date:** [August 17, 2021, 9:36pm UTC](https://forum.xojo.com/t/dont-use-tointeger-or-toint64/65067/20 "2021-08-17T21:36:24Z")

</div>

Together with the SQLite database problem that is now TWO serious errors. Let’s hope they get a new bug fix release out soon.

[Next page](https://forum.xojo.com/t/dont-use-tointeger-or-toint64/65067.md?page=2)
