DON'T USE .ToInteger or ToInt64!

When I see “3.0e+2” I expect a double, not an integer. Then I’m not really surprise that ToInt return 3 instead of 300. For me, an integer has not any other characters than 0123456789 . Why should it manage decimal separator as integer does not have decimal?

3 Likes

ToInt is supposed to convert TO AN INTEGER, and NOT accept only integers.

More importantly, why would you expect ToInt to be incapable of converting a valid numeric value like 3.0e2 aka 300 aka an INTEGER to an Integer??? Why would you expect a DOUBLE???

Has Math education completely vanished from the curriculum?

This is just embarrassing - and then someone “likes” it? :man_facepalming:

Even if you you are right about the case you write about here. And I agree with a lot you say,. Where is it okay for you to insult people?

I know you have a temper and get carried away somethimes, but please, there is no need to insult someone, be it a single person or a group of people.

11 Likes

I don’t have a temper, but I do despair at people who have a brain but don’t use it. In that case I tend to get really sarcastic. And to be quite honest, they deserved that.

You might disagree and prefer a “We all like each other, hold hands, and sing Kumbayah” approach, and I will defend your right to do that, but by the same token I defend my approach.

And what Thomas wrote made zero sense. But I am intrigued that someone LIKED his post … on what grounds? Certainly not Logic or Maths …

:slight_smile: maybee people like post thats is not yours beacuse you tend to be sarcastic.

Singing Kumbayah and hold hands certainly can be fun in the right circumstances :slight_smile:

1 Like

3.0e+2 = 300.0

Would you expect 300.0 to turn into 3 ?

1 Like

In my case, simply because it hit a letter. Would you expect “3,000” to turn into 3 or 3000? Why or why not?

As I recall even VB6 quit when it hit a letter or separator. I may remember wrong, that has been decades for me. However, I have no idea how VB6’s Val() treated scientific notation. Because in over 40 years of programming, the apps I work with have never had to deal with scientific notation.

But in general, I don’t like to use Val() unless in specific situations where I know all it will contain are digits. And so I will treat .ToInteger the same way, and use my own function or extension to convert a string value.

2 Likes

To add data to the discussion:

var stringValue as String = "3.0e+2"

var integerValue as Integer = stringValue.ToInteger '// = 3
var doubleValue as Double = stringValue.ToDouble '// = 300

var variantValue as Variant = stringValue
var integerValue2 as Integer = variantValue.IntegerValue '// = 300
var doubleValue2 as Double = variantValue.DoubleValue '// = 300

I, personally, probably wouldn’t use ToInteger if I were working with scientific notation to begin with.

4 Likes

This is not well-stated. It should say along the lines of: “Digits in the string are converted until the first non-digit is encountered, at which point the conversion ceases (digits are 0-9 inclusive).”

That makes it perfectly clear what “3e2” will be converted to. Well, it was perfectly clear before, Markus is just being obtuse.

No, you’re being obtuse, Markus. I was right the first time.

Personally, I would say that if a user wants to put the number 300 in an input field, that’s what they put. They don’t put 3e2 and then turn around looking for applause for being clever.

Who’s to say that the data came from an input field. It could be data being imported from a file / socket.

Since these functions can handle strings containing hex and binary values there should be no reason why they shouldn’t handle scientific notation.

1 Like

Tim
As someone who writes software for use in a scientific arena and have to deal with output from instruments, m
Markus has a very valid point IMO

2 Likes

All the more reason to never trust Val() (and now also .ToInteger) – because unless you KNOW exactly what can be contained in the data you should never trust Val. The docs for Val even say say as much:

For localized number formatting, use the CDbl function instead. Generally, you will use Val for converting internal data and use CDbl for converting data for input and output of user data.

This thread started with the OP saying to not use .ToInteger and I don’t disagree. But not just because of scientific notation. I also don’t like to use Val() on any string that could come from a user or unknown source.

You cannot trust CDbl any more than Val. That Xojo statement should probably say something like:
• Val - when the input always uses a decimal point as a decimal separator.
• CDbl - when the input uses the operating system’s decimal separator.

If the user has typed the input then CDbl will probably be better than Val (as long as the input matches the computer’s regional settings).

If the input is not from the user typing (file / socket) then you need to confirm what the decimal separator is as both Val and CDbl could produce the wrong result.

It still doesn’t change the fact that 3e2 is a valid integer so should be getting converted correctly.

1 Like

Double have in some country . to separate decimal and , to separate thousand. In other country it is the opposite. 4e0.5 = 2 (square root). What about 4e0,5 ?
As Douglas said, we have to test and check result when we convert string in complex number (double). If I use ConvertToInteger, I expect to manage string containing only number digit.
For me 3e2 is a double and should be managed as a double. Give a paper which contains only whole numbers to developers without say anything, they will create Dim MyArray(-1) as integer. Give them an array with whole numbers wrote 3e2 6e3 etc. , they will create a array as double.

Usually, people tell to other they are silly when we speak politics. And in a way, it is the case here. We’re all agree to say that 3e2 is a whole number then a integer, but we all know that the program will have to manage another number, wrote XeY how to manage them?

Off topic: This is not correct

4e0.5 = 4*10^0.5

Ahgh yes, sorry, it is 4^0.5 . 4e0.5 = 4*10^0.5 = 4 * 3,1623 = 12.649 and is not a whole number. What’s the expected result when converted to integer? 12 or 13 or ???

The expected behavior is the same as i in Var i As Integer = 12.649, what in some languages they call truncate()

Feedback Status: Fixed

4 Likes

This explains a LOT of my recent issues!!!

There seems to be some confusion about Val() vs. CDbl, and what to expect them to parse.

The basic idea is this:

  • If you output a number as a String using Str(), then you get it back via Val(). This is what you’d use if you write numbers to a text file, and being able to read them back again.
  • If you show numbers in the UI, to the user, you’d turn them into Strings using Format(), and then read the input from the user back using CDbl, assuming they want to use their native format.

Now, the question is what the inverse function to String.ToInteger is, i.e. the function that converts an Integer (or Int64) to a String. If that does only use digits and no scientific notation, then it would complement ToInteger. If that string generation function would turn very large numbers using scientific notation, though, then this would be a severe bug, indeed. Otherwise, it’s just a “be aware that ToInteger will not handle scientific notation” documentation issue.

2 Likes