Search
Search titles only
By:
Search titles only
By:
Log in
Register
Search
Search titles only
By:
Search titles only
By:
Menu
Install the app
Install
Forums
New posts
All threads
Latest threads
New posts
Trending threads
Trending
Search forums
What's new
New posts
New ads
New profile posts
Latest activity
Free Ads
Latest reviews
Search ads
Members
Current visitors
New profile posts
Search profile posts
Contact us
Latest ads
Colombo
Red Hat Certified System Administrator (RHCSA) - RHEL 10
Sanjeewani95
Updated:
Friday at 7:43 PM
NURSING , CAREGIVER , HOTEL & BEAUTY COURSES
IVA Para Medical Campus
Updated:
Thursday at 9:24 AM
Handmade Character Soft Toys Peppa Pig Family
anil1961
Updated:
Wednesday at 9:58 PM
Ad icon
Video Content Creator
pramukag
Updated:
Jun 28, 2026
Ad icon
QA Engineer Intern
pramukag
Updated:
Jun 28, 2026
Electronics
Vehicles
Property
Search
Reply to thread
Forums
Computers & Internet
Tips & Tricks
Tips & Tricks Collection of DC
Get the App
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Message
<blockquote data-quote="||~DxxCxxxx~||" data-source="post: 3691342" data-attributes="member: 137393"><p><strong>How do I round-off numbers using JavaScript?</strong></p><p></p><p><img src="http://upload.wikimedia.org/wikipedia/commons/thumb/8/8e/WikiBookTitel_JavaScript.jpg/400px-WikiBookTitel_JavaScript.jpg" alt="" class="fr-fic fr-dii fr-draggable " style="" /></p><p></p><p><span style="font-family: 'Tahoma'"><span style="font-size: 10px"><strong>It is easy, you can even round numbers off to a certain number of decimal places.</strong></span></span></p><p></p><p> <strong><span style="font-family: 'Tahoma'"><span style="font-size: 10px">JavaScript offers the following method: </span></span><span style="font-family: 'Courier New'"><span style="font-size: 10px"><span style="color: #ff0000">Math.round(x)</span></span></span></strong><span style="font-family: 'Tahoma'"><span style="font-size: 10px"></span></span></p><p><span style="font-family: 'Tahoma'"><span style="font-size: 10px"> </span></span></p><p><span style="font-family: 'Tahoma'"><span style="font-size: 10px"> </span></span><span style="font-family: 'Courier New'"><span style="font-size: 10px">Math.round(295.9); // returns 296</span></span></p><p><span style="font-family: 'Courier New'"><span style="font-size: 10px"> Math.round(245.2); // returns 245</span></span></p><p><span style="font-family: 'Courier New'"><span style="font-size: 10px"> Math.round(-102.58); // returns -103</span></span><span style="font-family: 'Tahoma'"><span style="font-size: 10px"></span></span></p><p><span style="font-family: 'Tahoma'"><span style="font-size: 10px"> </span></span></p><p><span style="font-family: 'Tahoma'"><span style="font-size: 10px"> <strong>Here is how you round to specific decimal numbers:</strong></span></span></p><p><span style="font-family: 'Tahoma'"><span style="font-size: 10px"> </span></span></p><p><span style="font-family: 'Tahoma'"><span style="font-size: 10px"> Use the </span></span><strong><span style="font-family: 'Courier New'"><span style="font-size: 10px"><span style="color: #ff0000">Math.round()</span></span></span></strong><span style="font-family: 'Tahoma'"><span style="font-size: 10px"> function along with the basic multiply and divide operators and you can easily round numbers to a specific decimal.</span></span></p><p><span style="font-family: 'Tahoma'"><span style="font-size: 10px"> </span></span></p><p><span style="font-family: 'Tahoma'"><span style="font-size: 10px"> For example: </span></span></p><p><span style="font-family: 'Tahoma'"><span style="font-size: 10px"> </span></span></p><p><span style="font-family: 'Tahoma'"><span style="font-size: 10px"> </span></span><span style="font-family: 'Courier New'"><span style="font-size: 10px">var pi = "3.1415926535";</span></span></p><p><span style="font-family: 'Courier New'"><span style="font-size: 10px"> </span></span></p><p><span style="font-family: 'Courier New'"><span style="font-size: 10px"> // Round to tenths</span></span></p><p><span style="font-family: 'Courier New'"><span style="font-size: 10px"> pi = Math.round(pi*10)/10; // returns 3.14</span></span></p><p><span style="font-family: 'Courier New'"><span style="font-size: 10px"> </span></span></p><p><span style="font-family: 'Courier New'"><span style="font-size: 10px"> // Round to hundredths</span></span></p><p><span style="font-family: 'Courier New'"><span style="font-size: 10px"> pi = Math.round(pi*100)/100; // returns 3.142</span></span></p><p><span style="font-family: 'Courier New'"><span style="font-size: 10px"> </span></span></p><p><span style="font-family: 'Courier New'"><span style="font-size: 10px"> // Round to Bazillionths</span></span></p><p><span style="font-family: 'Courier New'"><span style="font-size: 10px"> pi = Math.round(pi*infinity)/infinity; // Just Kidding</span></span><span style="font-family: 'Tahoma'"><span style="font-size: 10px"></span></span></p><p><span style="font-family: 'Tahoma'"><span style="font-size: 10px"> </span></span></p><p><span style="font-family: 'Tahoma'"><span style="font-size: 10px"> </span></span></p><p><span style="font-family: 'Tahoma'"><span style="font-size: 10px"> <strong>This function is very useful when rounding dollar amounts:</strong></span></span></p><p><span style="font-family: 'Tahoma'"><span style="font-size: 10px"> </span></span></p><p><span style="font-family: 'Tahoma'"><span style="font-size: 10px"> </span></span><span style="font-family: 'Courier New'"><span style="font-size: 10px">var totalDue = "1003.1415926535";</span></span></p><p><span style="font-family: 'Courier New'"><span style="font-size: 10px"> </span></span></p><p><span style="font-family: 'Courier New'"><span style="font-size: 10px"> // Round to tenths:</span></span></p><p><span style="font-family: 'Courier New'"><span style="font-size: 10px"> totalDue = Math.round(totalDue*100)/100; // returns $1003.14</span></span></p><p><span style="font-family: 'Courier New'"><span style="font-size: 10px"></span></span></p></blockquote><p></p>
[QUOTE="||~DxxCxxxx~||, post: 3691342, member: 137393"] [b]How do I round-off numbers using JavaScript?[/b] [IMG]http://upload.wikimedia.org/wikipedia/commons/thumb/8/8e/WikiBookTitel_JavaScript.jpg/400px-WikiBookTitel_JavaScript.jpg[/IMG] [FONT=Tahoma][SIZE=2][B]It is easy, you can even round numbers off to a certain number of decimal places.[/B][/SIZE][/FONT] [B][FONT=Tahoma][SIZE=2]JavaScript offers the following method: [/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#ff0000]Math.round(x)[/COLOR][/SIZE][/FONT][/B][FONT=Tahoma][SIZE=2] [/SIZE][/FONT][FONT=Courier New][SIZE=2]Math.round(295.9); // returns 296 Math.round(245.2); // returns 245 Math.round(-102.58); // returns -103[/SIZE][/FONT][FONT=Tahoma][SIZE=2] [B]Here is how you round to specific decimal numbers:[/B] Use the [/SIZE][/FONT][B][FONT=Courier New][SIZE=2][COLOR=#ff0000]Math.round()[/COLOR][/SIZE][/FONT][/B][FONT=Tahoma][SIZE=2] function along with the basic multiply and divide operators and you can easily round numbers to a specific decimal. For example: [/SIZE][/FONT][FONT=Courier New][SIZE=2]var pi = "3.1415926535"; // Round to tenths pi = Math.round(pi*10)/10; // returns 3.14 // Round to hundredths pi = Math.round(pi*100)/100; // returns 3.142 // Round to Bazillionths pi = Math.round(pi*infinity)/infinity; // Just Kidding[/SIZE][/FONT][FONT=Tahoma][SIZE=2] [B]This function is very useful when rounding dollar amounts:[/B] [/SIZE][/FONT][FONT=Courier New][SIZE=2]var totalDue = "1003.1415926535"; // Round to tenths: totalDue = Math.round(totalDue*100)/100; // returns $1003.14 [/SIZE][/FONT] [/QUOTE]
Insert quotes…
Verification
Nawa warak dahaya keeyada? (Namaya wadi kireema dahaya)
Post reply
Top
Bottom