Ir al contenido principal

Javascript Rounding Operations

Learning the different kinds of rounding operations can have many applications. In this article I will explain what each rounding application in JavaScript does. Even though the code may change on different programming languages, they all do the same.

Math.floor();

Rounds the number down 
Examples:
  1. Math.floor(1.5) = 1
  2. Math.floor(2.7) = 2
  3. Math.floor(1.3) = 1

Math.ceil();

Rounds the number up 
Examples:
  1. Math.ceil(1.5) = 2
  2. Math.ceil(2.7) = 3
  3. Math.ceil(1.3) = 2

Math.round();

Rounds the number to the closest integer
Examples:
  1. Math.round(1.5) = 2
  2. Math.round(2.7) = 3
  3. Math.round(1.3) = 1

Comentarios

Entradas más populares de este blog

All Possible Color Combinations

A display can project 16,777,216 different colors . One way to generate all color combinations is by using the same method employed on this article but with Hexadecimal numbers . If we count from 0 to 16,777,215(#000000 to #FFFFFF) and convert each number to hexadecimal we will generate all of the 16,777,216 colors . See this method in action by moving the slider below. See the Pen All Colors by LiveUser ( @liveuser ) on CodePen . By using this method to generate each color and assigning them to individual pixels I was able to produce a 8192x2048(16777216) pixel image. Click/tap here to see full resolution image

Hexadecimal to Decimal

To convert a hexadecimal to decimal all we have to do is do is perform the opposite procedure we used to convert to hex. In this example we will turn back the number that we used here(#35) into decimal. First we must use the table to get back again the Remainder Decimal value Hex Equivalent 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 A 11 B 12 C 13 D 14 E 15 F Note: each hexadecimal digit represents the Remainders found when converting the number from decimal to hexadecimal  Now revert the Process by performing these simple steps:  Use the  table above to find the decimal equivalent of each Remainder in the case of #35 is 3 5 Multiply each of the remainders(decimal equivalent of each hex character) by 16 to the power of "n" where "n" is the position of each character. The position of the character is found by counting from right to left and starting the count on 0 rather than 1. Note: the ...

How to learn how to code?

How I got Started My interest in video games(being a gamer) lead me to ask myself a couple of years ago "How do I make video games?". I ended up downloading a game engine called Unity 3d in which I found a Text editor with something called JavaScript . I Googled "What is JavaScript?" and "How to learn JavaScript?"  and ended up in a website called CodeCademy which had a very well structured interactive course. From there I started my coding journey  by reading online docs and different websites learning the basics of web development(HTML, CSS and JS) which lead me to learn how much knowledge I lacked in order to be able to develop video games. Developing video games was a lot harder and complex than I thought, I needed  to acquire 3d modeling skills, learn about texturing and animation, learn at least the basics of game engines and their specific functions for interacting with the scene and the elements using the programming language(scripting), audio r...