Integer Literals:base-10 integer literals、base-16 integer literals、base-8 integer literals
var a = 23; var b = 0x1A;//hexadecimal var c = 012;//octal console.log('a=' + a + ',b=' + b + ',c=' +c ); //a=10,b=26,c=10
Since some implementations support octal literals and some don't , you should never write an integer literal with a leading zero;you cannot know in this case whether an implementation will interpret it as an octal or decimal value.In the strict mode of ECMASCRIPT5,octal literals are explicitly forbidden.
Floating-Point Literals:integral part、decimal point、fractional part.May also be represented using exponential notation:a real number followed by the letter e(E),followed by optional plus or minus sign,followed by an integer exponent.
console.log(3.14); console.log('3.14e-2=' + 3.14e-2); //3.14 //3.14e2=0.0314 var a=38.8; var b=6.8; console.log('(a-b)=' + (a-b)); var a=134.22; var b=6; console.log('(a*b)=' + a*b); //(a-b)=31.999999999999996 //(a*b)=805.3199999999999
Solution 1:toFixed(),指定小数的精度
console.log('(a-b)=' + (a-b).toFixed(2)); console.log('(a*b)=' + (a*b).toFixed(2)); //(a-b)=32.00 //(a*b)=805.32
Solution 2:小数点转成整数运算后再做整数的降级,实际这种方法还是有问题
console.log(Number(-33.34).add(1.77)) //-31.570000000000004
Reason:there are infinitely many real numbers, but only a finite number of them can be represented exactly by the JavaScript floating-point format.This means that when you're working with real numbers in JavaScript, the representation of the number will often be an approximation of the actual number.
Because of rounding error, the difference between the approximations of .3 and .2 is not exactly the same as the difference between the approximations of .2 and .1.It is important to understand that this problem is not specific to JavaScript:it affects any programming languages that uses binary floating-point numbers.
A future version of JavaScript may support a decimal numeric type that avoids these rounding issues.Until then you might want to perform critical financial calculations using scaled integers.For example, you might manipulate monetary values as integer cents rather than fractional dollars.
Arithmetic:Javascript programs work with numbers using the arithmetic operators.In addition to these basic arithmetic operators, it supports more complex mathematical operations through a set of functions and constants defined as properties of Math object.For example:
Arithmetic in JavaScript doen not raise errors in cases of overflow, underflow, or division by zero.
- "negative zero" is almost completely indistinguishable from regular zero and JavaScript programmers rarely need to detect it.
- Division by zero is not an error in JS:it simply returns infinity or negative infinity.there is one exception, however:zero divided by zero does not hava a well-defined value,the result of this operation is the special not-a-number value, printed as NaN.
Number.POSITIVE_INFINITY Number.MAX_VALUE Number.NaN Number.NEGATIVE_INFINITY Number.MIN_VALUE // Infinity,-Infinity,NaN // console.log(isNaN(111)) console.log(isNaN(NaN)) console.log(isFinite(Number.POSITIVE_INFINITY)) console.log(Number.POSITIVE_INFINITY == Infinity)ECMAScript3,Infinity and NaN are read/write values and can be changed.ECMAScript5 corrects this and makes the values read-only.The Number object defines alternatives that are read-only even in ECMAScript3.
相关推荐
标题"test_JS-数字精确计算_"暗示我们将讨论如何在JavaScript中实现对小范围数字的精确计算。 描述中提到,JavaScript中的浮点数在运算过程中可能会被舍入,丢失部分精度。这是因为浮点数实际上是以二进制表示的,...
- **类型转换**:自动处理PHP和JavaScript之间的数据类型转换,如PHP的整型、浮点型到JavaScript的数字,以及PHP的字符串到JavaScript的字符串等。 - **错误处理**:提供错误处理机制,当PHP和JavaScript之间的通信...
`js-schema` 是一个专门用于JavaScript对象验证的开源库,它为开发者提供了简单且直观的API,使得在前端进行数据校验变得更加便捷。本文将深入探讨`js-schema`的核心功能、使用方法以及其在实际开发中的应用场景。 ...
首先,我们需要理解JavaScript的数据类型,如数字(number)和字符串(string),以及如何通过变量(variable)来存储数据。例如,我们可以定义一个变量来保存当前显示的数字,并通过改变这个变量的值来更新显示。 ...
- **自动识别排序类型**:`js-table-sorter`能自动判断表格中的数据类型,如数字、日期或字符串,并据此进行排序。 - **多列排序**:支持多列排序,可以通过多次点击不同的表头来切换排序依据。 - **自定义排序**...
本文将详细探讨“前端面试题之baseJS-==隐式类型转换”这一主题,帮助你掌握JavaScript中的类型转换规则,以便在面试中能够自信地解答相关问题。 在JavaScript中,“==”双等号运算符用于比较两个值是否相等。然而...
在学习JavaScript时,数字格式类型是一个重要的基础知识。首先,JavaScript中的数字主要分为整数和浮点数,整数可以是正整数、负整数或零。浮点数则包含小数点或使用科学记数法表示的数字。 在JavaScript中,十进制...
文中强调,在BigInt被提出前,因JS中所有数字基于双精度64位浮点数存储,所以在处理超限(超过2^53 - 1)的大整数时存在精度丢失的问题。随后阐述了BigInt能正确表示任意大整数,并给出创建方式,包含后缀'n'及...
在JavaScript(JS)编程语言中,变量类型和计算是学习任何编程语言的基础,尤其是在前端开发领域,JS扮演着至关重要的角色。本章将深入探讨JS中的变量、数据类型以及基本计算操作,帮助开发者掌握JS的核心概念。 ...
1. **基础知识**:书中首先介绍了JavaScript的基础概念,如变量、数据类型(字符串、数字、布尔值等)、条件语句(if-else)、循环(for、while)等。这些是编程的基础,也是后续学习的关键。 2. **p5.js入门**:...
- **变量定义与使用**:解释了在JavaScript中如何定义和使用各种类型的变量,如数字、布尔值等。 #### 3.3 JavaScript核心类型 - **Number类型**:介绍了JavaScript中的Number类型,包括其属性和方法。 - **Date...
《深入理解crypto-js4.1.1:JavaScript加密库在前端安全中的应用》 在现代Web开发中,数据安全已经成为至关重要的环节。特别是在JavaScript环境中,由于其代码的开放性,如何保护用户信息不被窃取或篡改成为了一个...
JavaScript支持多种数据类型,如数字(Number)、字符串(String)和布尔值(Boolean)等。 2. **Math对象**:JavaScript的Math对象包含各种数学常量和方法,其中`Math.random()`用于生成0到1之间(不包括1)的随机...
- 当传递变量时,确保它们是JSON序列化友好的,即只有Python的基本类型(字符串、数字、列表、字典等)。 - 考虑到安全性和性能,只将必要的数据暴露给前端,避免敏感信息泄露。 - 在生产环境中,为了防止XSS攻击,...
有个项目需要用到jquery 的easyui和导出excel,发现官方下载的datagrid-export.js有几处bug,例如导出的excel格式,长数字会默认按科学计数法显示或没了零开头的数字,不符合所见即所得要求。 首先说明前提条件,...
首先,我们要理解JavaScript的基本语法,包括变量声明(var、let、const)、数据类型(如字符串、数字、布尔、null、undefined、对象、数组)、运算符(算术、比较、逻辑、赋值等)、流程控制(条件语句if...else、...
### JavaScript 10分钟速成 (js-in-ten-minutes) #### 概述 本指南由JavaScript专家Spencer Tipping撰写,旨在为已有一定JavaScript基础的学习者提供一个快速掌握高级特性的途径。对于那些了解其他函数式语言(如...
2. **数据类型验证**:检查数据是否为特定类型,如`email`(邮箱格式)、`url`(URL格式)、`integer`(整数)、`numeric`(数字)等。 3. **正则表达式匹配**:自定义复杂规则,如通过正则表达式验证手机号码、...
无依赖性 宽平台兼容性:仅使用 JavaScript 1.5 (ECMAScript 3) 功能全面的文档和测试集包括一个 TypeScript 声明文件:decimal.d.ts 该库类似于 bignumber.js,但这里的精度是根据有效数字而不是小数位指定的,并且...
* 数字/数值类型:例如 `var a = 10;` * 字符串类型:例如 `var s = 'hello';` * 布尔类型:例如 `var b = true;` * 特殊的类型:null 代表空,undefined 代表未定义,NaN 代表不是一个数值 5. 运算符:js 中有多种...