`

JavaScript Rounding Methods of the Math object

阅读更多

    The next group of methods has to do with rounding decimal values into integers. Three methods — Math.ceil(),  Math.floor(), and  Math.round() — handle rounding in different ways as
described here:
    The  Math.ceil() method represents the ceiling function, which always rounds numbers up
to the nearest integer value.
    The  Math.floor() method represents the fl  oor function, which always rounds numbers
down to the nearest integer value.

    The  Math.round() method represents a standard round function, which rounds up if the
number is at least halfway to the next integer value (0.5 or higher) and rounds down if not.
This is the way you were taught to round in elementary school.

    The following example illustrates how these methods work:

alert(Math.ceil(25.9));     //26
alert(Math.ceil(25.5));     //26
alert(Math.ceil(25.1));     //26

                   
alert(Math.round(25.9));    //26
alert(Math.round(25.5));    //26
alert(Math.round(25.1));    //25

        
alert(Math.floor(25.9));    //25
alert(Math.floor(25.5));    //25
alert(Math.floor(25.1));    //25

 

 

 

分享到:
评论

相关推荐

    elative Error P r o p a g a t i o n in the R e c u r s i v e S o l u t i o n

    An important method for the systematic evaluation of functions obeying a linear recurrence relation with respect to one of their parameters is to solve the relation recursively, making use of known ...

    Bootstrap 4 responsive web design

    Rounding the charts 223 Creating a quick statistical card 226 Getting a spider chart 229 Overhead loading 232 Fixing the toggle button for mobile 234 Summary 235 Chapter 10: Bringing Components to ...

    Introduction_to_Optimum_Design.pdf

    The document "Introduction_to_Optimum_Design.pdf" delves deeply into the principles and methods of optimum design, which is a critical area within engineering and applied mathematics. This field ...

    A Smarter Way to Learn JavaScript.pdf

    日期和时间的操作在JavaScript中也有其特有的方法,本书中讲解了获取当前日期和时间(Getting the current date and time)、提取日期和时间的各个部分(Extracting parts of the date and time)、指定日期和时间...

    Academci Reference of MUN

    RULE 3: Quorum A two-thirds majority which is calculated by dividing the number of all Member States in plenary sessions or Committees by three and multiply by two (rounding up on fractions) shall ...

    A samart way to learn javascript

    30. Extracting parts of the date and time(提取日期和时间的部分):从日期对象中提取特定部分,如年、月、日、小时等。 31. Specifying a date and time(指定日期和时间):如何创建一个指定的日期和时间对象。...

    二级减速器课程设计说明书reducer design specification.doc

    According to the selected fullload speed of the motor nm and the drive shaft speed of the motor nw,we can calculate the total transmission ratio of the transmission device ia: (2)Allocate ...

    计算机组成与结构体系英文课件:Chapter9 Arithmetic

    Chapter 9 of the "Computer Organization and Architecture" course delves into the fundamental arithmetic operations performed by computer systems. The chapter focuses on the representation and ...

    Packt.Augmented.Reality.Game.Development.1787122883

    Rounding out, the technical material will open a discussion of further development that will present a multiplayer version of the game. At the end, you will be presented with troubleshooting ...

    python3.6.5参考手册 chm

    The json module: JavaScript Object Notation The plistlib module: A Property-List Parser ctypes Enhancements Improved SSL Support Deprecations and Removals Build and C API Changes Port-Specific ...

    Rounding Profile简介

    NULL 博文链接:https://sap.iteye.com/blog/295523

    Modelling.software.with.pictures.Practical.UML.diagramming.for.real-time.systems

    A key aspect in all of this is the sensible application of a set of diagrams defined within the Unified Modelling Language (UML) standard. It is aimed at those designing - or who intend to design - ...

    CheatEngine71.zip

    Additions and changes: Added support for il2cpp (mono...The chosen floating point rounding type is now saved in the registry You can now use (addresslist description) as an address Improved autocomplete

    kgb档案压缩console版+源码

    the archived file, and the offset of the first difference is reported. The decompressor requires as much memory as was used to compress. There is no option. It is not possible to add, remove, or ...

    Rounding:舍入程序

    Java提供了一系列的类和方法来实现不同类型的舍入操作,这些操作主要集中在`java.lang.Math`类和`java.math.RoundingMode`枚举类型中。 首先,让我们了解Java中的基本数据类型和运算符。Java提供了多种数值类型,...

    MIPS32 仿真器源码

    (It omits most floating point comparisons and rounding modes and the memory system page tables.) The MIPS architecture has several variants that differ in various ways (e.g., the MIPS64 architecture ...

    Lerner -- Python Workout. 50 Essential Exercises -- 2020.pdf

    The exercises cover various aspects of the Python programming language, including basic syntax, data structures, algorithms, and working with external formats such as CSV and JSON. #### Detailed ...

    Exp_MA_Rounding_Channel - MetaTrader 5EA.zip

    《MetaTrader 5 EA:基于MA_Rounding_Channel指标的突破交易系统详解》 在金融交易领域,自动交易系统,特别是外汇市场,已经成为许多交易者首选的工具。MetaTrader 5(MT5)作为一款功能强大的交易平台,为交易者...

Global site tag (gtag.js) - Google Analytics