`
mingren135
  • 浏览: 69257 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

JS-数字类型

 
阅读更多

 

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.

  1. "negative zero" is almost completely indistinguishable from regular zero and JavaScript programmers rarely need to detect it.
  2. 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. 

 

 

  • 大小: 9.6 KB
  • 大小: 38.2 KB
分享到:
评论

相关推荐

    Web前端开发技术-认识JavaScript的数据类型.pptx

    基本数据类型-数字型;认识JavaScript的数据类型;认识JavaScript的数据类型;认识JavaScript的数据类型;认识JavaScript的数据类型;基本数据类型-字符串型;认识JavaScript的数据类型;认识JavaScript的数据类型;认识...

    javascript学习笔记--数字格式类型

    很多人也许只知道 123,123.456,0xff 之类的数字格式。其实 js 格式还有很多数字格式类型,比如 1., .1 这样的,也有 .1e2 这样的。

    js-confetti:dependencies零依赖的JS Confetti库

    类型 描述 默认值 五彩纸屑半径 数字 五彩纸屑形状的半径(以像素为单位) 10 五彩纸屑数 数字 发射的纸屑数量(每侧N / 2-左右) 200 jsConfetti . addConfetti ( { confettiRadius : 10 , confett

    文章目录 一、javaScript 的几种数据类型 1、数字类型 2、字符串 3、布尔型 4、数组 5、null 和 undef

    JavaScript 中的数字类型指整数和浮点数,不刻意区分二者。 JavaScript 还支持16进制的整数,在所要表示的16进制数前面加上 0x 或者 0X 即可,如 0x1f 表示整数31。 对于较大或较小的数,JavaScript 还支持科学...

    decimal.js - JavaScript的任意精度Decimal类型-javascript

    无依赖性 宽平台兼容性:仅使用 JavaScript 1.5 (ECMAScript 3) 功能全面的文档和测试集包括一个 TypeScript 声明文件:decimal.d.ts 该库类似于 bignumber.js,但这里的精度是根据有效数字而不是小数位指定的,并且...

    CODEWARS-JAVASCRIPT-PUZZLES:来自www.codewars.com的Java编码难题

    卡塔5-训练JS#2-基本数据类型-数字 卡塔6-Trainingi JS#3-基本数据类型-字符串 卡塔7-训练JS#4-基本数据类型-数组 卡塔8-训练JS#5-基本数据类型-对象 卡塔9-训练JS#6-基本数据类型-布尔和条件语句,如果......

    JavaScript基础语法资料 JavaScript代码 素材 笔记 作业资料.zip

    JavaScript基础语法资料 JavaScript代码 素材 笔记 作业资料,学习资料 01-编程语言.html 02-JS初体验.html 03-JS注释.html 04-JS输入输出语句.html 05-变量.html 06-变量案例.html 07-变量案例弹出用户名.html 08-...

    javascript数字类型基础知识.md

    javascript数据类型基础知识: 将其他类型转换为number类型 - Number([val]) ... <从左往右读取字符串,直到遇到非数据,将读取的数据转化为数字类型> - 字符串与1相乘 (将字符串快速转化为数值)

    generic-graphic:使用react-chartjs-2适用于任何图形类型的通用组件

    组件通用图形以使用react-chartjs-2 组件接收道具type为图形的类型, data具有所述图形信息, width和height 。 options键包括其他可自定义的内容,例如工具提示,标签,轴等。 信息, width和height 。 options键...

    JavaScript-js宝典笔学习记.txt

    7.JS中的值类型:String,Number,Boolean,Null,Object,Function 8.JS中的字符型转换成数值型:parseInt(),parseFloat() 9.JS中的数字转换成字符型:(""+变量) 10.JS中的取字符串长度是:(length) 11.JS中的字符与字符相...

    js中将具有数字属性名的对象转换为数组

    虽然不太常用,但我们的确可以给对象添加以数字为属性名的属性: 代码如下: var obj = {}; obj[0] = 1; obj[1] = 2; 这个对象并不是数组类型,那有没有办法把它转换为数组类型呢?jQuery代码中采用了Array.prototype...

    js-bson:JavaScript BSON 实现

    由于 JavaScript 语言缺乏对 Int32+ 数字的支持,因此 Int64 的编码和解码方式不同。 JS代码JavaScript 代码。 带作用域的 JS 代码带有变量的 JavaScript 代码。 空值空 JavaScript 关键字。 正则表达式正则表达式...

    libphonenumber-js-utils

    Libphonenumber-js-utils· 它是什么 此库中实用程序功能的编译后的缩小版本。 这些函数在浏览器环境中公开给...获取输入的数字类型,例如FIXED_LINE , MOBILE等。 getValidationError 功能 获取验证错误 isValidN

    JS 能表示的最大数值及JS Number类型数字位数及IEEE754标准1

    大整数的精度丢失和浮点数本质上是一样的,尾数位最大是 52 位,因此 JS 中能精准表示的最大整数是 Math.pow(2, 53),十进制即大于 900719

    js根据数字字符串自动获取范围区间.txt

    js根据数字字符串自动获取范围区间,用于数字类型的值自动获取查询范围,默认分为五个区间,如需选择其他范围区间,修改getScope方法中initScope的参数

    javascript-intro-to-looping-js-intro-000

    解释JS中每种循环类型之间的区别 介绍 有时,我们需要在生活中反复做事-例如我们的日常工作。 我们每天早晨醒来。 我们不得不反复上班或上学。 我们反复决定接下来要在YouTube / Netflix上观看什么。 在编程中,...

    JavaScript判断输入是否为数字类型的方法总结

    JavaScript判断输入是否为数字类型的方法总结 前言 很多时候需要判断一个输入是否位数字,下面简单列举集中方法。 第一种方法 isNaN isNaN 返回一个 Boolean 值,指明提供的值是否是保留值 NaN (不是数字)。  ...

    js完美解决jsp文本框限制只能输入数字、小数问题

    js完美解决jsp文本框限制只能输入数字、小数问题,只需 ;" oncontextmenu="return false;" onKeyDown="keyNumDown(this, '8', '2')"> oncontextmenu="return false;" oncontextmenu="return false;":防止右键粘贴

    Python -数字累加的执行流程图

    Python还被语言流行指数的编译器Tiobe将它被评为最受欢迎的编程语言,20多年来首次将Python置于Java、C和JavaScript之上,真的非常荣幸了. 自从20世纪90年代初Python语言诞生至2022年,它已被逐渐广泛应用于系统...

    Javascript的数据类型转换

    parseInt()-将字符串转化为整型 parseFloat()-将字符串转化为浮点数 ...String()-函数能够将任何类型的值转换为字符串 tostring()-将数字转换为进制 Boolean()-转换为Boolean值 toFixed-()将数值转换为字符串

Global site tag (gtag.js) - Google Analytics