Numbers Conversion

Data Types

Numbers Conversion

🙋 Need help? Ask an expert now!

There are a few different number conversations that are good to remember and learn about as you move on with the course. You can convert floating-point to integers, integers to rounded integers, and strings to integers as shown below. These will be extremely helpful as we start writing more complicated programs and methods.

The code above shows all three types of conversions we can use in our program. The first conversion from floating-point to integer is used when rounding is not considered. The conversion from floating-point to integer truncates the decimal. The digits that come after the decimal point are ignored and the original number is not rounded during the process. If you want to round the floating point to an integer, all you have to do is to add 0.5 to the original value as shown above. This will round your floating-point 100% of the time.

Converting strings to integer requires a similar method of using | 0. If the string doesn't start with a digit, the conversion will not work.

Edit Me on GitHub!

Application Question

What will the following code segment print on the console?

let varOne = 10.55;
let varTwo = varOne | 0;