All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Float To Integer JavaScript

Last Updated : Mar 11, 2024

Float To Integer JavaScript

In this tutorial we will show you the solution of float to integer JavaScript, here we are going to show you example for conversion float to integer using Math.round() method.

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.

Step By Step Guide On Float To Integer JavaScript :-

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>
  1. <!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.
  2. The<html> tag is used to indicate the beginning of HTML document.
  3. 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.
  4. 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.
  5. <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
  6. In script we defined two variables ‘v1, v2’ with respective specific values ‘345.189, 673.894’ by using ‘var’ keyword.
  7. 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.
  8. 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.
  9. Both </body>,</html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.

Conclusion :-

In conclusion now we are able to know how to do sum with array objects in javascript.

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.

I hope this tutorial on float to integer JavaScript helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪