Conversion

I ran out of light this evening before I could get an answer, so I need to ask why this does not convert as I would expect.

Var DuskSensor As Integer = Dictionary_Temperature.Value(“Dusk Sensor”)*100

If
System.DebugLog “Dusk Sensor % = “+ Format(Dictionary_Temperature.Value(“Dusk Sensor”),”##%”)
yields 25%

Should not
System.DebugLog "Dusk Sensor Integer= "+ Str(Dictionary_Temperature.Value(“Dusk Sensor”)*100)
yield 25

The Dictionary is a Variant and you are using 100 so it “guesses” that the type is integer and not double.

You can change 100 to 100.0 (even just 100. ) or do Ctype(dictionary.value, double) to get 25.

It looks like the Format ##% expects or do a calculation as double, for that reason format gives you what you expect and str does not.

Try this:

System.DebugLog "Dusk Sensor Integer= "+ Str(Dictionary_Temperature.Value("Dusk Sensor")*100.0)

or

System.DebugLog "Dusk Sensor Integer= "+ Str(Dictionary_Temperature.Value("Dusk Sensor")*100.)

That’s not how your code reads. In the First statement, you are missing the multiplier:

  • 100

Whereas in the Str statement, you do.

FWIW, Str can take a format string too.

I’m sorry Greg, I don’t understand what you are saying.

The “First statement” is this?

System.DebugLog “Dusk Sensor % = “+ Format(Dictionary_Temperature.Value(“Dusk Sensor”),”##%”)

From https://docs.xojo.com/Format

Character: %
Description: Displays the number multiplied by 100.

We get this for 25 and 0.25:

System.DebugLog Format(25,"##%")     //2500%
System.DebugLog Format(0.25, "##%")  //25%

I’ll find out tomorrow when the sun comes out. I had tried to declare DuskSensor As Double but that wasn’t the answer.

Var DuskSensor As Integer = Dictionary_Temperature.Value(“Dusk Sensor”)*100
to
Var DuskSensor As Double = Dictionary_Temperature.Value(“Dusk Sensor”)*100

I will change it to:
Var DuskSensor As Integer = Dictionary_Temperature.Value(“Dusk Sensor”)*100.0

Thanks

I think Xojo changes the Variant according to the operation and not to the defined var, in other words, even if you change it to As Double the operation will still be Integer.

or store the measurement with *100.0 direct (as example 0-100 if source is 0-1) so you do not need to convert it later.

1 Like

As mentioned above here’s the documentation about it: Variant - Xojo Documentation under Automatic conversion of values

Variants used in an expression convert themselves to the type of the other operand, no matter what kind of data they contain, instead of deferring the type-conversion until runtime.

So in:

Var DuskSensor As Integer = Dictionary_Temperature.Value("Dusk Sensor")*100

the value from Dictionary_Temperature.Value("Dusk Sensor") will be an Integer as you’re using 100 (integer) instead of 100.0 (double). 0.25 as an integer will be 0 so you’ve got 0*100 = 0

The correct way would be:

Var DuskSensor As Integer = Dictionary_Temperature.Value("Dusk Sensor").DoubleValue * 100

as double * integer will be worked out as a double then the answer of that converted to an integer to store in the DuskSensor variable.

or

Var DuskSensor As Integer = Dictionary_Temperature.Value("Dusk Sensor") * 100.0

so you have double * double with the answer being converted to an Integer

The reason why:

System.DebugLog "Dusk Sensor % = "+ Format(Dictionary_Temperature.Value(“Dusk Sensor”),”##%”)

works is because you’re always working with a double (what is stored in the dictionary and what Format expects) and never going near an Integer.

Where as:

System.DebugLog "Dusk Sensor Integer= "+ Str(Dictionary_Temperature.Value("Dusk Sensor")*100)

is again converting Dictionary_Temperature.Value("Dusk Sensor") to an Integer because you’re multiplying by an integer.

2 Likes

You are correct. I’d forgotten about the percent formatter.

Perhaps the better question is: what does

Str(Dictionary_Temperature.Value(“Dusk Sensor”)*100)

Result in?

For value of 0.25 the result is 0
For value of 25 the result is 2500

Thanks to @JulianS for pointing the right docs and clearly explain the reason.

1 Like

Great exclamation of the problem guys. It’s nice to have a breakdown as a teacher might so us less knowledgeable might better understand a concept. I also had no idea about ctype which is interesting for converting integers to boolean. Useful stuff.

// Convert Integer to Boolean
// 0 converts to False
// non-zero converts to True
Var i As Integer = 42

Var b As Boolean
b = CType(i, Boolean)

Var b As Boolean
b = CType(i, Boolean)

P.S. When the sun came out this morning all was well!

I was actually able to fined 10 places in my home automation software that I was unknowingly converting temperatures stored in a dictionary to integers. CType fixed that issue.

Forum for Xojo Programming Language and IDE. Copyright © 2021 Xojo, Inc.