How to always round a number UP to the nearest penny in JavascriptHow to deal with floating point number precision in JavaScript?How to round an integer up or down to the nearest 10 using JavascriptJavascript: formatting a rounded number to N decimalsHow to print a number with commas as thousands separators in JavaScriptGet decimal portion of a number with JavaScriptHow to round up a number in Javascript?javascript - how to round to nearest integerHow can I add new array elements at the beginning of an array in Javascript?Javascript and rounding both positive and negative numbers to nearest 1000Round to nearest X

Why didn't Thanos kill all the Dwarves on Nidavellir?

Managing and organizing the massively increased number of classes after switching to SOLID?

How might the United Kingdom become a republic?

Get ids only where one id is null and other isn't

The monorail explodes before I can get on it

Is Trump personally blocking people on Twitter?

How did the hit man miss?

Was the Ford Model T black because of the speed black paint dries?

Is an acid a salt? Why (not)?

Do you know your 'KVZ's?

Can I call 112 to check a police officer's identity in the Czech Republic?

How to hide what's behind an object in a non destructive way / give it an "invisibility cloak"

Print the last, middle and first character of your code

What's the maximum time an interrupt service routine can take to execute on atmega328p?

Does Google Maps take into account hills/inclines for route times?

Supporting developers who insist on using their pet language

Can I play a first turn Simic Growth Chamber to have 3 mana available in the second turn?

If the railway suggests a 5-min connection window for changing trains in the Netherlands, does that mean it's definitely doable?

Does throwing a penny at a train stop the train?

<schwitz>, <zwinker> etc. Does German always use 2nd Person Singular Imperative verbs for emoticons? If so, why?

Should I intentionally omit previous work experience when applying for jobs?

Professor falsely accusing me of cheating in a class he does not teach, two months after end of the class. What precautions should I take?

Why were Er and Onan punished if they were under 20?

During copyediting, journal disagrees about spelling of paper's main topic



How to always round a number UP to the nearest penny in Javascript


How to deal with floating point number precision in JavaScript?How to round an integer up or down to the nearest 10 using JavascriptJavascript: formatting a rounded number to N decimalsHow to print a number with commas as thousands separators in JavaScriptGet decimal portion of a number with JavaScriptHow to round up a number in Javascript?javascript - how to round to nearest integerHow can I add new array elements at the beginning of an array in Javascript?Javascript and rounding both positive and negative numbers to nearest 1000Round to nearest X






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















I have a bunch of numbers, for example 797.3333333333334, 852.22222111, 933.111023, which I want to ALWAYS round up to the nearest penny, such that the numbers I already mentioned would be 797.34, 852.23, 933.12, respectively.



I said the nearest penny, but you might also call it the nearest tenth.



There is a ceiling function, but that only rounds to the nearest integer, as does Math.round()










share|improve this question
























  • ceil doesn't round to nearest integer, it gets the first integer larger or equal to your number.

    – Cristy
    Nov 22 '14 at 22:28

















1















I have a bunch of numbers, for example 797.3333333333334, 852.22222111, 933.111023, which I want to ALWAYS round up to the nearest penny, such that the numbers I already mentioned would be 797.34, 852.23, 933.12, respectively.



I said the nearest penny, but you might also call it the nearest tenth.



There is a ceiling function, but that only rounds to the nearest integer, as does Math.round()










share|improve this question
























  • ceil doesn't round to nearest integer, it gets the first integer larger or equal to your number.

    – Cristy
    Nov 22 '14 at 22:28













1












1








1








I have a bunch of numbers, for example 797.3333333333334, 852.22222111, 933.111023, which I want to ALWAYS round up to the nearest penny, such that the numbers I already mentioned would be 797.34, 852.23, 933.12, respectively.



I said the nearest penny, but you might also call it the nearest tenth.



There is a ceiling function, but that only rounds to the nearest integer, as does Math.round()










share|improve this question
















I have a bunch of numbers, for example 797.3333333333334, 852.22222111, 933.111023, which I want to ALWAYS round up to the nearest penny, such that the numbers I already mentioned would be 797.34, 852.23, 933.12, respectively.



I said the nearest penny, but you might also call it the nearest tenth.



There is a ceiling function, but that only rounds to the nearest integer, as does Math.round()







javascript






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 24 '15 at 21:32







maudulus

















asked Nov 22 '14 at 22:26









maudulusmaudulus

5,2955 gold badges46 silver badges83 bronze badges




5,2955 gold badges46 silver badges83 bronze badges












  • ceil doesn't round to nearest integer, it gets the first integer larger or equal to your number.

    – Cristy
    Nov 22 '14 at 22:28

















  • ceil doesn't round to nearest integer, it gets the first integer larger or equal to your number.

    – Cristy
    Nov 22 '14 at 22:28
















ceil doesn't round to nearest integer, it gets the first integer larger or equal to your number.

– Cristy
Nov 22 '14 at 22:28





ceil doesn't round to nearest integer, it gets the first integer larger or equal to your number.

– Cristy
Nov 22 '14 at 22:28












3 Answers
3






active

oldest

votes


















7














The Math.ceil(x) function returns the smallest integer greater than or equal to a number "x".



var rounded = Math.ceil(yourNumber * 100)/100;





share|improve this answer






























    1














    Just do it like this: Math.ceil(number * 100) / 100.






    share|improve this answer






























      0














      Properly rounding to the nearest penny:



      var yourNumber = 5.495;
      yourNumber = Math.round(yourNumber * 100)/100;
      alert(yourNumber);


      Always round up to the nearest penny:



      function precision(a) 
      if (!isFinite(a)) return 0;
      var e = 1, p = 0;
      while (Math.round(a * e) / e !== a)
      e *= 10; p++;

      return p;

      if (precision(yourNumber) >= 3)
      yourNumber = (Math.trunc(yourNumber * 100)/100) * 1 + 0.01;






      share|improve this answer

























      • maudulus, Do you have any resources on how to manage garbage collection in html5 canvas?

        – Super-WhyDoYouWantToDoThat-Man
        Mar 26 at 14:03













      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f27083235%2fhow-to-always-round-a-number-up-to-the-nearest-penny-in-javascript%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      7














      The Math.ceil(x) function returns the smallest integer greater than or equal to a number "x".



      var rounded = Math.ceil(yourNumber * 100)/100;





      share|improve this answer



























        7














        The Math.ceil(x) function returns the smallest integer greater than or equal to a number "x".



        var rounded = Math.ceil(yourNumber * 100)/100;





        share|improve this answer

























          7












          7








          7







          The Math.ceil(x) function returns the smallest integer greater than or equal to a number "x".



          var rounded = Math.ceil(yourNumber * 100)/100;





          share|improve this answer













          The Math.ceil(x) function returns the smallest integer greater than or equal to a number "x".



          var rounded = Math.ceil(yourNumber * 100)/100;






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 22 '14 at 22:29









          hereandnow78hereandnow78

          10.4k6 gold badges36 silver badges47 bronze badges




          10.4k6 gold badges36 silver badges47 bronze badges























              1














              Just do it like this: Math.ceil(number * 100) / 100.






              share|improve this answer



























                1














                Just do it like this: Math.ceil(number * 100) / 100.






                share|improve this answer

























                  1












                  1








                  1







                  Just do it like this: Math.ceil(number * 100) / 100.






                  share|improve this answer













                  Just do it like this: Math.ceil(number * 100) / 100.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 22 '14 at 22:30









                  MouagipMouagip

                  3,3602 gold badges26 silver badges45 bronze badges




                  3,3602 gold badges26 silver badges45 bronze badges





















                      0














                      Properly rounding to the nearest penny:



                      var yourNumber = 5.495;
                      yourNumber = Math.round(yourNumber * 100)/100;
                      alert(yourNumber);


                      Always round up to the nearest penny:



                      function precision(a) 
                      if (!isFinite(a)) return 0;
                      var e = 1, p = 0;
                      while (Math.round(a * e) / e !== a)
                      e *= 10; p++;

                      return p;

                      if (precision(yourNumber) >= 3)
                      yourNumber = (Math.trunc(yourNumber * 100)/100) * 1 + 0.01;






                      share|improve this answer

























                      • maudulus, Do you have any resources on how to manage garbage collection in html5 canvas?

                        – Super-WhyDoYouWantToDoThat-Man
                        Mar 26 at 14:03















                      0














                      Properly rounding to the nearest penny:



                      var yourNumber = 5.495;
                      yourNumber = Math.round(yourNumber * 100)/100;
                      alert(yourNumber);


                      Always round up to the nearest penny:



                      function precision(a) 
                      if (!isFinite(a)) return 0;
                      var e = 1, p = 0;
                      while (Math.round(a * e) / e !== a)
                      e *= 10; p++;

                      return p;

                      if (precision(yourNumber) >= 3)
                      yourNumber = (Math.trunc(yourNumber * 100)/100) * 1 + 0.01;






                      share|improve this answer

























                      • maudulus, Do you have any resources on how to manage garbage collection in html5 canvas?

                        – Super-WhyDoYouWantToDoThat-Man
                        Mar 26 at 14:03













                      0












                      0








                      0







                      Properly rounding to the nearest penny:



                      var yourNumber = 5.495;
                      yourNumber = Math.round(yourNumber * 100)/100;
                      alert(yourNumber);


                      Always round up to the nearest penny:



                      function precision(a) 
                      if (!isFinite(a)) return 0;
                      var e = 1, p = 0;
                      while (Math.round(a * e) / e !== a)
                      e *= 10; p++;

                      return p;

                      if (precision(yourNumber) >= 3)
                      yourNumber = (Math.trunc(yourNumber * 100)/100) * 1 + 0.01;






                      share|improve this answer















                      Properly rounding to the nearest penny:



                      var yourNumber = 5.495;
                      yourNumber = Math.round(yourNumber * 100)/100;
                      alert(yourNumber);


                      Always round up to the nearest penny:



                      function precision(a) 
                      if (!isFinite(a)) return 0;
                      var e = 1, p = 0;
                      while (Math.round(a * e) / e !== a)
                      e *= 10; p++;

                      return p;

                      if (precision(yourNumber) >= 3)
                      yourNumber = (Math.trunc(yourNumber * 100)/100) * 1 + 0.01;







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Mar 26 at 6:57









                      maudulus

                      5,2955 gold badges46 silver badges83 bronze badges




                      5,2955 gold badges46 silver badges83 bronze badges










                      answered Mar 26 at 3:22









                      Super-WhyDoYouWantToDoThat-ManSuper-WhyDoYouWantToDoThat-Man

                      365 bronze badges




                      365 bronze badges












                      • maudulus, Do you have any resources on how to manage garbage collection in html5 canvas?

                        – Super-WhyDoYouWantToDoThat-Man
                        Mar 26 at 14:03

















                      • maudulus, Do you have any resources on how to manage garbage collection in html5 canvas?

                        – Super-WhyDoYouWantToDoThat-Man
                        Mar 26 at 14:03
















                      maudulus, Do you have any resources on how to manage garbage collection in html5 canvas?

                      – Super-WhyDoYouWantToDoThat-Man
                      Mar 26 at 14:03





                      maudulus, Do you have any resources on how to manage garbage collection in html5 canvas?

                      – Super-WhyDoYouWantToDoThat-Man
                      Mar 26 at 14:03

















                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f27083235%2fhow-to-always-round-a-number-up-to-the-nearest-penny-in-javascript%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                      Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                      Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript