简体   繁体   English

如何将两个浮点数填充到其中一个的精度?

[英]How to fill two floats to precision of one of them?

I have two numbers, for example:我有两个数字,例如:

var a = $('input#number_1').val(); // 12;
var b = $('input#number_2').val(); // 0.01;

How to fill a to precision of b?如何填充a到b的精度?

a: 12.01
b: 0.01

or fill both to for example:或填写两者,例如:

a: 12.0000
b: 0.0100

Edit:编辑:

var a = parseFloat($('input#number_1').val()); // 12;
var b = parseFloat($('input#number_2').val()); // 0.01;

The first thing to realize is that in your code, a and b are strings , not numbers.首先要意识到的是,在您的代码中, ab字符串,而不是数字。

What you could do is convert them both to number, the do a simple toString , find out how many digits are after the .你可以做的是将它们都转换为数字,做一个简单的toString ,找出. in each of them, and then use the larger of those numbers with toFixed on the numbers (which lets you say how many places after the decimal you want).在每个数字中,然后使用这些数字中较大的一个,并在数字上使用toFixed (它可以让您说出您想要的小数点后多少位)。

For example:例如:

 var a = $('input#number_1').val(); var b = $('input#number_2').val(); // Convert them to numbers var aValue = +a; var bValue = +b; // Find out which has more digits after the decimal var maxDigits = 0; var n; a = String(aValue); n = a.indexOf('.'); if (n !== -1) { maxDigits = a.length - n - 1; } b = String(bValue); n = b.indexOf('.'); if (n !== -1) { maxDigits = Math.max(b.length - n - 1, maxDigits); } // Convert back to strings with that many of digits a = aValue.toFixed(maxDigits); b = bValue.toFixed(maxDigits); // Put them back in the fields if that's what you want $('input#number_1').val(a); $('input#number_2').val(b);
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <input id="number_1" value="12"> <input id="number_2" value="0.01">

try this javascript toFixed function, which is used for fix decimal value .试试这个 javascript toFixed 函数,它用于修复十进制值。 you can set number of decimal value to function.您可以设置十进制值的数量以发挥作用。 make sure what you are passing.确保你正在通过什么。 you may change it as per your requirement,您可以根据您的要求更改它,

 var num = 5; var n = num.toFixed(2) alert(n); num = 5.6785; n = num.toFixed(2) alert(n);

Refrence: http://www.w3schools.com/jsref/jsref_tofixed.asp参考: http : //www.w3schools.com/jsref/jsref_tofixed.asp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Javascript中解压缩半精度浮点数 - Decompressing Half Precision Floats in Javascript 当您有很多文本框时,如何只填充它们 - How to fill only one textbox from a modal when you have a lot of them 单击其中之一时如何切换两个图像? - how to toggle two image when I click one of them? 如何并排放置两个跨度,然后将一个跨度放在它们上面 - How to place two spans side by side and then one span on top of them AngularJS如何从一个网站运行两个控制器,并更改其中一个 - AngularJS How would one run two controllers from one website, and have one of them change 比较 JavaScript 中的两个浮点数 - Compare two floats in JavaScript 在javascript中添加两个浮点数 - Adding two floats in javascript 我如何比较表单中的两个日期并验证它们? 第二个不应该在第一个之前 - How would I compare two dates in a form and validate them? The second one shouldn't be before the first one 按两个值排序,优先考虑其中之一 - Sort by two values prioritizing on one of them 如何在容器中放置两个div,以便在调整另一个div的大小时自动调整其大小以填充容器? - How can I have two divs inside a container such that when resizing one the other automatically resizes to fill the container?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM