本文翻译自:How to parse float with two decimal places in javascript?

I have the following code. 我有以下代码。 I would like to have it such that if price_result equals an integer, let's say 10, then I would like to add two decimal places. 我想这样,如果price_result等于一个整数,让我们说10,那么我想添加两个小数位。 So 10 would be 10.00. 所以10将是10.00。 Or if it equals 10.6 would be 10.60. 或者,如果它等于10.6将是10.60。 Not sure how to do this. 不知道该怎么做。

price_result = parseFloat(test_var.split('$')[1].slice(0,-1));

#1楼

参考:https://stackoom.com/question/Ibn0/如何在javascript中解析带有两个小数位的浮点数


#2楼

I've got other solution. 我有其他解决方案。

You can use round() to do that instead toFixed() 您可以使用round()代替toFixed()

var twoPlacedFloat = parseFloat(yourString).round(2)

#3楼

When you use toFixed , it always returns the value as a string. 使用toFixed ,它始终将值作为字符串返回。 This sometimes complicates the code. 这有时会使代码复杂化。 To avoid that, you can make an alternative method for Number. 为避免这种情况,您可以为Number创建替代方法。

Number.prototype.round = function(p) {p = p || 10;return parseFloat( this.toFixed(p) );
};

and use: 并使用:

var n = 22 / 7; // 3.142857142857143
n.round(3); // 3.143

or simply: 或者干脆:

(22/7).round(3); // 3.143

#4楼

Try this (see comments in code): 试试这个(见代码中的注释):

function fixInteger(el) {// this is element's value selector, you should use your ownvalue = $(el).val();if (value == '') {value = 0;}newValue = parseInt(value);// if new value is Nan (when input is a string with no integers in it)if (isNaN(newValue)) {value = 0;newValue = parseInt(value);}// apply new value to element$(el).val(newValue);
}function fixPrice(el) {// this is element's value selector, you should use your ownvalue = $(el).val();if (value == '') {value = 0;}newValue = parseFloat(value.replace(',', '.')).toFixed(2);// if new value is Nan (when input is a string with no integers in it)if (isNaN(newValue)) {value = 0;newValue = parseFloat(value).toFixed(2);}// apply new value to element$(el).val(newValue);
}

#5楼

To return a number, add another layer of parentheses. 要返回一个数字,请添加另一层括号。 Keeps it clean. 保持清洁。

var twoPlacedFloat = parseFloat((10.02745).toFixed(2));

#6楼

If you need performance (like in games): 如果你需要表演(比如游戏):

Math.round(number * 100) / 100

It's about 100 times as fast as parseFloat(number.toFixed(2)) 它大约是parseFloat的100倍(number.toFixed(2))

http://jsperf.com/parsefloat-tofixed-vs-math-round http://jsperf.com/parsefloat-tofixed-vs-math-round

如何在javascript中解析带有两个小数位的浮点数?相关推荐

  1. 取出url中的字符_如何在JavaScript中解析URL:例如主机名,路径名,查询,哈希?...

    统一资源定位符(缩写URL)是对Web资源(网页,图像,文件)的引用.URL指定资源位置和检索资源的机制(http,ftp,mailto). 例如,这是此博客文章的URL: 通常,您需要访问URL的特 ...

  2. !! javascript_产量! 产量! 生成器如何在JavaScript中工作。

    !! javascript by Ashay Mandwarya ?️?? 由Ashay Mandwarya提供吗? 产量! 产量! 生成器如何在JavaScript中工作. (Yield! Yiel ...

  3. 如何在JavaScript中区分深层副本和浅层副本

    by Lukas Gisder-Dubé 卢卡斯·吉斯杜比(LukasGisder-Dubé) 如何在JavaScript中区分深层副本和浅层副本 (How to differentiate betw ...

  4. 如何在JavaScript中使用apply(?),call(?)和bind(➰)方法

    by Ashay Mandwarya ?️?? 由Ashay Mandwarya提供吗? 如何在JavaScript中使用apply(?),call(?)和bind(➰)方法 (How to use ...

  5. 如何在JavaScript中声明名称空间?

    如何在JavaScript中创建名称空间,以使我的对象和函数不会被其他同名对象和函数覆盖? 我使用了以下内容: if (Foo == null || typeof(Foo) != "obje ...

  6. 如何在 JavaScript 中获取当前日期?

    问: 想要改进这篇文章?提供这个问题的详细答案,包括引文和解释为什么你的答案是正确的.没有足够细节的答案可能会被编辑或删除. 如何在 JavaScript 中获取当前日期? 答1: HuntsBot周 ...

  7. 我如何在JavaScript中建立良好的发布过程

    by Dafna Rosenblum 达夫娜·罗森布拉姆(Dafna Rosenblum) 我如何在JavaScript中建立良好的发布过程 (How I established a good rel ...

  8. 如何在JavaScript中检测用户的首选配色方案

    by Oskar Hane 由Oskar Hane 如何在JavaScript中检测用户的首选配色方案 (How to detect a user's preferred color scheme i ...

  9. 如何在JavaScript中实现链接列表

    If you are learning data structures, a linked list is one data structure you should know. If you do ...

最新文章

  1. web直播方案总结:
  2. 基于Android Studio搭建hello world工程
  3. 2019-11-13 惯性环节怎么写成m语言
  4. 把远程仓库的项目,clone到eclipse里面
  5. 爬取异步请求(XHR/JS)数据方法
  6. ubuntu安装配置方法【转】
  7. postman上传图片时已经添加cookie,但仍显示未登陆
  8. 前端开发那些不常见但十分有效的小玩意
  9. Java基础(六)——容器
  10. 早上起床后喝一杯白开水是非常有好处的
  11. jquery学习之事件委派
  12. 正态分布里的西格玛_七大数据陷阱之油腻的统计学:正态分布来了
  13. 第五讲 C#中的异常处理
  14. spring framework源码下载并导入eclipse
  15. DEV CPP中使用Clang
  16. Oracle 建表语句,表结构操作sql
  17. 15个Python游戏小项目
  18. 【JAVA】初识Java
  19. < 知识拓展:CSS 中常用的计量单位有哪些? >
  20. SDL_ttf库显示字体

热门文章

  1. CH Round #72树洞[二分答案 DFSBFS]
  2. Atitit.研发管理如何避免公司破产倒闭的业务魔咒
  3. asp.net core刷新css缓存
  4. Jmeter命令行执行并生成HTML报告
  5. hihocoder#1513 : 小Hi的烦恼
  6. transform 动画效果
  7. Sublime 编译运行JavaScript
  8. SQL SERVER日期函数详细用法
  9. MS SQL Server:分区表、分区索引详解
  10. Emit学习-进阶篇-异常处理