rodriggj / Go

0 stars 0 forks source link

1.5 Lets convert a value #5

Open rodriggj opened 2 years ago

rodriggj commented 2 years ago

Type Conversion

Changes the type of a Variable Declaration to another type. (aka Casting)

The type conversion express is like this:

type(value)
rodriggj commented 2 years ago

Example 1 - Convert a Float to Int

  1. Suppose you declare 2 variables speed and force. When you assign initial values to these variables using the short declaration method, you find these values are typed differently. One is a whole number and is typed as an int, the other is a decimal which is typed as float64.
package main

import (
    "fmt"
)

func main() {
    speed := 5   //speed is currently typed as an int
    force := 2.5 //force is currenlty typeda as float64

    speed = speed * force

    fmt.Println("speed:", speed)
}

The VS Code will emit an error, because there is a type mismatch.

  1. To fix this issue of type mismatch you have 2 choices. 1. you can cast speed to another data type or 2. you can cast force to another data type.

First try to cast force to an int...

func main() {
    speed := 5   //speed is currently typed as an int
    force := 2.5 //force is currenlty typeda as float64

    speed = speed * int(force)

    fmt.Println("speed:", speed)
}

Results in ...

NOTE: The standard rounding logic of the next integer if greater than 5 will round the value upward DOES NOT apply and therefore float conversion can be a DESTRUCTIVE OPERATION. In this case 2.5 does not round to 3, it simply remains at int value of 2 and the decimal precision is LOST.

NOTE: If you want to see this conversion happen, add an additional line to the code


fmt.Println("force", int(force))
rodriggj commented 2 years ago

Example 2 - Convert an Int to a Float -- The first value you convert matters

  1. So the above example shows the decision criteria for converting a Float to an Int with the tradespace being a potentially destructive operation. Lets evaluate what happens if we choose to convert an int to a float.
package main

import (
    "fmt"
)

func main() {
    speed := 5   //speed is currently typed as an int
    force := 2.6 //force is currenlty typeda as float64

    speed = float64(speed) * force

    fmt.Println("speed:", speed)
}
  1. In this scenario, you still receive an error. Why? Because the current state of speed is still an int even though it has been cast as a float64. So casting to a float doesn't work. Instead you simply need to change the value of speed to 5.0
package main

import (
    "fmt"
)

func main() {
    speed := 5.0 //speed is currently typed as an int
    force := 2.6 //force is currenlty typeda as float64

    speed = speed * force

    fmt.Println("speed:", speed)
}

Results in ...

  1. You can also execute this type of conversion like this...
    
    package main

import ( "fmt" )

func main() { speed := 5 //speed is currently typed as an int force := 2.6 //force is currenlty typeda as float64

speed = int(float64(speed) * force)

fmt.Println("speed:", speed)

}


Which also results in 13.