Background
Break News
How to add local font to Tailwind Css and NextJS? - Tutorial Design Pattern? - Blockchain Technology, How to create own Bitcoin virtual currency - Zustand mordern management state - Design Pattern - Flyweight Pattern? - Docker Full training Topic

[Tips] Format number and Round Number with Javascript

Sunday, 9 September 2018
|
Read: Completed in minutes

[Tips] Format number and Round Number with Javascript

Hello everybody

Format number and Round Number with Javascript


Today is September 9, 2018 is a very special day, and I was also free, I would like to introduce to you rounded numbers in javascript,

In fact, the rounding technique is not strange, but master about it you need a certain amount of time, to help you save energy, today I decided to summarize and share my experience about rounding the numbers from decimal and integers, so that you are no longer confused by this problem anymore :)

Let's start!

If you search "round number with javascript" on google. Its will give you many website reference, but all website still focus, Math.floor, Math.ceil, Math.trunc ....

How to use floor, ceil, trunc, lets see the demo
 
 <script>
  function myFunction() {
      var a = Math.round(2.60);  // 3
      var b = Math.round(2.50);  // 3
      var c = Math.round(2.49);  // 2
      var d = Math.round(-2.60); // -3
      var e = Math.round(-2.50); // -2 
      var f = Math.round(-2.49); // -2
 
      var x = a + "<br>" + b + "<br>" + c + "<br>" + d + "<br>" + e + "<br>" + f;
      document.getElementById("demo").innerHTML = x; 
  }
  </script>

And IF YOU WANT TO round integer number, how do you can do it, example you have this number: 126333 -> you want to round it with result = 126000?, see, the rare website show you how to do it, but in Learn Tech Tips, you can learn that :D

function int_round_3_number(num) {  // lam tron phan nguyen
    return Math.floor(num / 1000) * 1000;
}
 
 
use function above, you can easy do it, try it!, surprise :D
int_round_3_number(126333) -> 126000
 
Have a nice day!
Zidane

πŸ™‡πŸΌπŸ™‡πŸΌ Your Feedback Is Valuable and Helpful to Us - Webzone, all things Tech Tips web development Zidane
Popular Webzone Tech Tips topic maybe you will be like it - by Webzone Tech Tips - Zidane
As a student, I found Blogspot very useful when I joined in 2014. I have been a developer for years . To give back and share what I learned, I started Webzone, a blog with tech tips. You can also search for tech tips zidane on Google and find my helpful posts. Love you all,

I am glad you visited my blog. I hope you find it useful for learning tech tips and webzone tricks. If you have any technical issues, feel free to browse my posts and see if they can help you solve them. You can also leave a comment or contact me if you need more assistance. Here is my blog address: https://learn-tech-tips.blogspot.com.

My blog where I share my passion for web development, webzone design, and tech tips. You will find tutorials on how to build websites from scratch, using hot trends frameworks like nestjs, nextjs, cakephp, devops, docker, and more. You will also learn how to fix common bugs on development, like a mini stackoverflow. Plus, you will discover how to easily learn programming languages such as PHP (CAKEPHP, LARAVEL), C#, C++, Web(HTML, CSS, javascript), and other useful things like Office (Excel, Photoshop). I hope you enjoy my blog and find it helpful for your projects. :)

Thanks and Best Regards!
Follow me on Tiktok @learntechtips and send me a direct message. I will be happy to chat with you.
Webzone - Zidane (huuvi168@gmail.com)
I'm developer, I like code, I like to learn new technology and want to be friend with people for learn each other
I'm a developer who loves coding, learning new technologies, and making friends with people who share the same passion. I have been a full stack developer since 2015, with more than years of experience in web development.
Copyright @2022(November) Version 1.0.0 - By Webzone, all things Tech Tips for Web Development Zidane
https://learn-tech-tips.blogspot.com