当前位置:首页>编程日记>正文

在JavaScript中使用正好两位小数格式化数字

我有这行代码将我的数字四舍五入到小数点后两位。 但是我得到这样的数字:10.8、2.4等。这些不是我对小数点后两位的想法,因此我如何改善以下内容?

Math.round(price*Math.pow(10,2))/Math.pow(10,2);

我想要数字10.80、2.40等。使用jQuery对我来说很好。


#1楼

toFixed(n)提供小数点后的n个长度; toPrecision(x)提供x的总长度。

在下面使用此方法

// Example: toPrecision(4) when the number has 7 digits (3 before, 4 after)// It will round to the tenths placenum = 500.2349;result = num.toPrecision(4); // result will equal 500.2

并且,如果您希望该号码固定使用

result = num.toFixed(2);

#2楼

@heridev和我在jQuery中创建了一个小函数。

您可以尝试下一个:

的HTML

<input type="text" name="one" class="two-digits"><br>
<input type="text" name="two" class="two-digits">​

jQuery的

// apply the two-digits behaviour to elements with 'two-digits' as their class
$( function() {$('.two-digits').keyup(function(){if($(this).val().indexOf('.')!=-1){         if($(this).val().split(".")[1].length > 2){                if( isNaN( parseFloat( this.value ) ) ) return;this.value = parseFloat(this.value).toFixed(2);}  }            return this; //for chaining});
});

网上演示:

http://jsfiddle.net/c4Wqn/


#3楼

我通常将其添加到我的个人库中,在提出一些建议并也使用@TIMINeutron解决方案之后,使其适用于十进制长度,这是最合适的:

function precise_round(num, decimals) {var t = Math.pow(10, decimals);   return (Math.round((num * t) + (decimals>0?1:0)*(Math.sign(num) * (10 / Math.pow(100, decimals)))) / t).toFixed(decimals);
}

将适用于所报告的异常。


#4楼

我没有找到解决此问题的准确方法,因此我创建了自己的解决方案:

function inprecise_round(value, decPlaces) {return Math.round(value*Math.pow(10,decPlaces))/Math.pow(10,decPlaces);
}function precise_round(value, decPlaces){var val = value * Math.pow(10, decPlaces);var fraction = (Math.round((val-parseInt(val))*10)/10);//this line is for consistency with .NET Decimal.Round behavior// -342.055 => -342.06if(fraction == -0.5) fraction = -0.6;val = Math.round(parseInt(val) + fraction) / Math.pow(10, decPlaces);return val;
}

例子:

 function inprecise_round(value, decPlaces) { return Math.round(value * Math.pow(10, decPlaces)) / Math.pow(10, decPlaces); } function precise_round(value, decPlaces) { var val = value * Math.pow(10, decPlaces); var fraction = (Math.round((val - parseInt(val)) * 10) / 10); //this line is for consistency with .NET Decimal.Round behavior // -342.055 => -342.06 if (fraction == -0.5) fraction = -0.6; val = Math.round(parseInt(val) + fraction) / Math.pow(10, decPlaces); return val; } // This may produce different results depending on the browser environment console.log("342.055.toFixed(2) :", 342.055.toFixed(2)); // 342.06 on Chrome & IE10 console.log("inprecise_round(342.055, 2):", inprecise_round(342.055, 2)); // 342.05 console.log("precise_round(342.055, 2) :", precise_round(342.055, 2)); // 342.06 console.log("precise_round(-342.055, 2) :", precise_round(-342.055, 2)); // -342.06 console.log("inprecise_round(0.565, 2) :", inprecise_round(0.565, 2)); // 0.56 console.log("precise_round(0.565, 2) :", precise_round(0.565, 2)); // 0.57 

http://www.coolblog.cn/news/1e65b93866efce52.html

相关文章:

  • asp多表查询并显示_SpringBoot系列(五):SpringBoot整合Mybatis实现多表关联查询
  • s7day2学习记录
  • 【求锤得锤的故事】Redis锁从面试连环炮聊到神仙打架。
  • 矿Spring入门Demo
  • 拼音怎么写_老师:不会写的字用圈代替,看到孩子试卷,网友:人才
  • Linux 实时流量监测(iptraf中文图解)
  • Win10 + Python + GPU版MXNet + VS2015 + RTools + R配置
  • 美颜
  • shell访问php文件夹,Shell获取某目录下所有文件夹的名称
  • 如何优雅的实现 Spring Boot 接口参数加密解密?
  • LeCun亲授的深度学习入门课:从飞行器的发明到卷积神经网络
  • Mac原生Terminal快速登录ssh
  • java受保护的数据与_Javascript类定义语法,私有成员、受保护成员、静态成员等介绍...
  • mysql commit 机制_1024MySQL事物提交机制
  • 支撑微博千亿调用的轻量级RPC框架:Motan
  • jquery 使用小技巧
  • 法拉利虚拟学院2010 服务器,法拉利虚拟学院2010
  • 2019-9
  • vscode pylint 错误_将实际未错误的py库添加到pylint白名单
  • 科学计算工具NumPy(3):ndarray的元素处理
  • linux批量创建用户和密码
  • js常用阻止冒泡事件
  • 工程师在工作电脑存 64G 不雅文件,被公司开除后索赔 41 万,结果…
  • newinsets用法java_Java XYPlot.setInsets方法代碼示例
  • 气泡图在开源监控工具中的应用效果
  • 各类型土地利用图例_划重点!国土空间总体规划——土地利用
  • php 启动服务器监听
  • dubbo简单示例
  • [iptables]Redhat 7.2下使用iptables实现NAT
  • 【设计模式】 模式PK:策略模式VS状态模式
  • Ubuntu13.10:[3]如何开启SSH SERVER服务
  • CSS小技巧——CSS滚动条美化
  • JS实现-页面数据无限加载
  • 阿里巴巴分布式服务框架 Dubbo
  • 最新DOS大全
  • Django View(视图系统)
  • 阿里大鱼.net core 发送短信
  • 程序员入错行怎么办?
  • Sorenson Capital:值得投资的 5 种 AI 技术
  • 两张超级大表join优化
  • 第九天函数
  • Linux软件安装-----apache安装
  • HDU 5988 最小费用流
  • 《看透springmvc源码分析与实践》读书笔记一
  • Arm芯片的新革命在缓缓上演
  • 正式开课!如何学习相机模型与标定?(单目+双目+鱼眼+深度相机)
  • nagios自写插件—check_file
  • python3 错误 Max retries exceeded with url 解决方法
  • 行为模式之Template Method模式
  • 通过Spark进行ALS离线和Stream实时推荐