The Math.round() method rounds a number to the nearest integer but rounds process differ based on floating point decimal value either increased with value ‘1’ or leaves as it whole digit before floating point as integer.
Here we defined two variables ‘v1,v2’ with respective specific values ‘345.189, 673.894’.
For convert specified float value into integer we used Math.round() method it will helps us to rounds our defined value into integer when we pass that variable to Math.round() method and lastly we printed both ‘v1, v2’ converted result of value at console panel.
<!DOCTYPE html> <html> <head> <title>FLOAT TO INTEGER</title> </head> <body> <script> var v1=345.189; var v2=673.894; console.log("INTEGER VALUE "+Math.round(v1)); console.log("INTEGER VALUE "+Math.round(v2)); </script> </body> </html>
- <!DOCTYPE html> tag which is instruct the web browser about what version of HTML file written in and it’s not have any ending tag.
- The<html> tag is used to indicate the beginning of HTML document.
- As above shown <head> tag is contain information about webpage and external file links are declared here. <title> tag is used for set the webpage title.
- Both <head> and <title> tags having their pair end tag, so we need to close the ending tags respectively. If you’re not closed anyone of ending tag properly that is also affect the webpage result.
- <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
- In script we defined two variables ‘v1, v2’ with respective specific values ‘345.189, 673.894’ by using ‘var’ keyword.
- When we passing variables ‘v1, v2’ to Math.round() method it returns nearest rounded integer value. Here we specified v1 value ‘345.189’ will returns ‘345’ as result because after floating point value is ‘189’ so it will leaves as it.
- When we had above ‘5’ at nearest floating point then Math.round() method will returns before floating point digits with increment by 1 as result otherwise it returns before floating point digits as it is that’s way variable v2’s value ‘673.894’ returns in this method ‘674’ value as result.
- Both </body>,</html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.
When we executes program on browser we can’t see the result because we printed on console panel so we have to use shortcut ‘ctrl+shift+j’ then console panel will open at right hand side with results of ‘INTEGER VALUE 345, INTEGER VALUE 674’.
If your need to define variable with some other values then you can modify it in this concept accordingly.