Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在不进行上舍入或下舍入的情况下获得精确的数字_Javascript_Jquery - Fatal编程技术网

Javascript 如何在不进行上舍入或下舍入的情况下获得精确的数字

Javascript 如何在不进行上舍入或下舍入的情况下获得精确的数字,javascript,jquery,Javascript,Jquery,我试图得到一个精度为2位小数的数字,例如,如果我有这些数字,这就是我想要的: 3.456 it must returns me 3.45 3.467 = 3.46 3.435 = 3.43 3.422 = 3.42 我不想为了得到我看到的2个位置的数字而向上或向下取整 谢谢 好的,这是答案 var a = 5.469923; var truncated = Math.floor(a * 100) / 100; // = 5.46 谢谢大家的帮助。假设为正数: 守则: function ro

我试图得到一个精度为2位小数的数字,例如,如果我有这些数字,这就是我想要的:

3.456 it must returns me 3.45
3.467 = 3.46
3.435 = 3.43
3.422 = 3.42
我不想为了得到我看到的2个位置的数字而向上或向下取整

谢谢


好的,这是答案

var a = 5.469923;
var truncated = Math.floor(a * 100) / 100; // = 5.46

谢谢大家的帮助。

假设为正数:

守则:

function roundDown(num,dec) {
    return Math.floor(num*Math.pow(10,dec))/Math.pow(10,dec);
}
测试:

function test(num, expected) {
    var val = roundDown(num,2);
    var pass = val === expected;
    var result =  pass ? "PASS" : "FAIL";
    var color = pass ? "GREEN" : "RED";
    console.log("%c" + result + " : " + num + " : " + val, "background-color:" + color);
}

test(3.456, 3.45);
test(3.467, 3.46);
test(3.435, 3.43);
test(3.422, 3.42);
基本思想:

  • 记下数字
  • 将数字相乘,将小数点移动到所需的有效位数
  • 请输入数字以删除尾随数字
  • 将数字除以以获得正确的值

如果你想有一个尾随的零,你需要使用
toFixed(2)
,这将把数字变成一个字符串

function roundDown(num,dec) {
    return Math.floor(num*Math.pow(10,dec))/Math.pow(10,dec).toFixed(2);
}
测试用例需要更改为

test(3.456, "3.45");
test(3.467, "3.46");
test(3.435, "3.43");
test(3.422, "3.42");

另一个选项是正则表达式

function roundDown(num,dec) {
    var x = num.toString().match(/(\d*(\.\d{2}))?/);
    return x ? parseFloat(x[0]) : "";
    //return x ? parseFloat(x[0]).toFixed(2) : "";
}

假设为正数:

守则:

function roundDown(num,dec) {
    return Math.floor(num*Math.pow(10,dec))/Math.pow(10,dec);
}
测试:

function test(num, expected) {
    var val = roundDown(num,2);
    var pass = val === expected;
    var result =  pass ? "PASS" : "FAIL";
    var color = pass ? "GREEN" : "RED";
    console.log("%c" + result + " : " + num + " : " + val, "background-color:" + color);
}

test(3.456, 3.45);
test(3.467, 3.46);
test(3.435, 3.43);
test(3.422, 3.42);
基本思想:

  • 记下数字
  • 将数字相乘,将小数点移动到所需的有效位数
  • 请输入数字以删除尾随数字
  • 将数字除以以获得正确的值

如果你想有一个尾随的零,你需要使用
toFixed(2)
,这将把数字变成一个字符串

function roundDown(num,dec) {
    return Math.floor(num*Math.pow(10,dec))/Math.pow(10,dec).toFixed(2);
}
测试用例需要更改为

test(3.456, "3.45");
test(3.467, "3.46");
test(3.435, "3.43");
test(3.422, "3.42");

另一个选项是正则表达式

function roundDown(num,dec) {
    var x = num.toString().match(/(\d*(\.\d{2}))?/);
    return x ? parseFloat(x[0]) : "";
    //return x ? parseFloat(x[0]).toFixed(2) : "";
}

假设为正数:

守则:

function roundDown(num,dec) {
    return Math.floor(num*Math.pow(10,dec))/Math.pow(10,dec);
}
测试:

function test(num, expected) {
    var val = roundDown(num,2);
    var pass = val === expected;
    var result =  pass ? "PASS" : "FAIL";
    var color = pass ? "GREEN" : "RED";
    console.log("%c" + result + " : " + num + " : " + val, "background-color:" + color);
}

test(3.456, 3.45);
test(3.467, 3.46);
test(3.435, 3.43);
test(3.422, 3.42);
基本思想:

  • 记下数字
  • 将数字相乘,将小数点移动到所需的有效位数
  • 请输入数字以删除尾随数字
  • 将数字除以以获得正确的值

如果你想有一个尾随的零,你需要使用
toFixed(2)
,这将把数字变成一个字符串

function roundDown(num,dec) {
    return Math.floor(num*Math.pow(10,dec))/Math.pow(10,dec).toFixed(2);
}
测试用例需要更改为

test(3.456, "3.45");
test(3.467, "3.46");
test(3.435, "3.43");
test(3.422, "3.42");

另一个选项是正则表达式

function roundDown(num,dec) {
    var x = num.toString().match(/(\d*(\.\d{2}))?/);
    return x ? parseFloat(x[0]) : "";
    //return x ? parseFloat(x[0]).toFixed(2) : "";
}

假设为正数:

守则:

function roundDown(num,dec) {
    return Math.floor(num*Math.pow(10,dec))/Math.pow(10,dec);
}
测试:

function test(num, expected) {
    var val = roundDown(num,2);
    var pass = val === expected;
    var result =  pass ? "PASS" : "FAIL";
    var color = pass ? "GREEN" : "RED";
    console.log("%c" + result + " : " + num + " : " + val, "background-color:" + color);
}

test(3.456, 3.45);
test(3.467, 3.46);
test(3.435, 3.43);
test(3.422, 3.42);
基本思想:

  • 记下数字
  • 将数字相乘,将小数点移动到所需的有效位数
  • 请输入数字以删除尾随数字
  • 将数字除以以获得正确的值

如果你想有一个尾随的零,你需要使用
toFixed(2)
,这将把数字变成一个字符串

function roundDown(num,dec) {
    return Math.floor(num*Math.pow(10,dec))/Math.pow(10,dec).toFixed(2);
}
测试用例需要更改为

test(3.456, "3.45");
test(3.467, "3.46");
test(3.435, "3.43");
test(3.422, "3.42");

另一个选项是正则表达式

function roundDown(num,dec) {
    var x = num.toString().match(/(\d*(\.\d{2}))?/);
    return x ? parseFloat(x[0]) : "";
    //return x ? parseFloat(x[0]).toFixed(2) : "";
}

使用字符串操作来实现它

var n = 4.56789;
var numbers = n.toString().split('.');
result = Number(numbers[0]+"."+numbers[1].substr(0,2));
alert(result);

使用字符串操作来实现它

var n = 4.56789;
var numbers = n.toString().split('.');
result = Number(numbers[0]+"."+numbers[1].substr(0,2));
alert(result);

使用字符串操作来实现它

var n = 4.56789;
var numbers = n.toString().split('.');
result = Number(numbers[0]+"."+numbers[1].substr(0,2));
alert(result);

使用字符串操作来实现它

var n = 4.56789;
var numbers = n.toString().split('.');
result = Number(numbers[0]+"."+numbers[1].substr(0,2));
alert(result);

您将数字视为一串数字,而不是单个值,因此请将其视为字符串-

function cutoff(n, cut){    
    var parts= String(n).split('.'), dec= parts[1];
    if(!cut) return parts[0];
    if(dec && dec.length>cut) parts[1]= dec.substring(0, cut);
    return parts.join('.');
}
var n= 36.938;
cutoff(n,2)

/*  returned value: (String)
36.93
*/

如果你想要一个数字,+cutoff(n,2)就可以了。

你把数字看成是一个数字串,而不是一个单一的值,所以把它当作一个字符串来对待-

function cutoff(n, cut){    
    var parts= String(n).split('.'), dec= parts[1];
    if(!cut) return parts[0];
    if(dec && dec.length>cut) parts[1]= dec.substring(0, cut);
    return parts.join('.');
}
var n= 36.938;
cutoff(n,2)

/*  returned value: (String)
36.93
*/
function truncateDec(num, decplaces) {
    return (num*Math.pow(10,decplaces) - num*Math.pow(10,decplaces) % 1)/Math.pow(10,decplaces);
}
alert(truncateDec(105.678, 2)); // Returns 105.67
alert(truncateDec(105.678, 1)); // Returns 105.6

如果你想要一个数字,+cutoff(n,2)就可以了。

你把数字看成是一个数字串,而不是一个单一的值,所以把它当作一个字符串来对待-

function cutoff(n, cut){    
    var parts= String(n).split('.'), dec= parts[1];
    if(!cut) return parts[0];
    if(dec && dec.length>cut) parts[1]= dec.substring(0, cut);
    return parts.join('.');
}
var n= 36.938;
cutoff(n,2)

/*  returned value: (String)
36.93
*/
function truncateDec(num, decplaces) {
    return (num*Math.pow(10,decplaces) - num*Math.pow(10,decplaces) % 1)/Math.pow(10,decplaces);
}
alert(truncateDec(105.678, 2)); // Returns 105.67
alert(truncateDec(105.678, 1)); // Returns 105.6

如果你想要一个数字,+cutoff(n,2)就可以了。

你把数字看成是一个数字串,而不是一个单一的值,所以把它当作一个字符串来对待-

function cutoff(n, cut){    
    var parts= String(n).split('.'), dec= parts[1];
    if(!cut) return parts[0];
    if(dec && dec.length>cut) parts[1]= dec.substring(0, cut);
    return parts.join('.');
}
var n= 36.938;
cutoff(n,2)

/*  returned value: (String)
36.93
*/
function truncateDec(num, decplaces) {
    return (num*Math.pow(10,decplaces) - num*Math.pow(10,decplaces) % 1)/Math.pow(10,decplaces);
}
alert(truncateDec(105.678, 2)); // Returns 105.67
alert(truncateDec(105.678, 1)); // Returns 105.6
如果您想要一个数字,+截止(n,2)就可以了

function truncateDec(num, decplaces) {
    return (num*Math.pow(10,decplaces) - num*Math.pow(10,decplaces) % 1)/Math.pow(10,decplaces);
}
alert(truncateDec(105.678, 2)); // Returns 105.67
alert(truncateDec(105.678, 1)); // Returns 105.6
如果不需要动态小数位数,则可以进一步简化

function truncateDec(num) {
    return (num*100 - num*100 % 1)/100;
}
alert(truncateDec(105.678)); // Returns 105.67

它是如何工作的?

其概念是,主截断通过将原始小数点除以1得到余数来工作。其余的将是小数点处的任何内容。余数运算符为%

105.678 % 1 = 0.678
通过从原始数字中减去这个余数,我们将只剩下整数

105.678 - 0.678 = 105
要包含
x
小数位数,我们需要首先将原始数字乘以该小数位数的幂10,从而将小数点向后移动
x
位置。在本例中,我们将采用
x=2

105.678 * 10^2 
= 105.678 * 100
= 10567.8
现在,我们通过再次减去余数来重复相同的过程

10567.8 % 1 = 0.8
10567.8 - 0.8 = 10567
为了返回到要求的位置数,我们将其除以10^x

10567 / 10^2
= 10567 / 100
= 105.67
希望有帮助

如果不需要动态小数位数,则可以进一步简化

function truncateDec(num) {
    return (num*100 - num*100 % 1)/100;
}
alert(truncateDec(105.678)); // Returns 105.67

它是如何工作的?

其概念是,主截断通过将原始小数点除以1得到余数来工作。其余的将是小数点处的任何内容。余数运算符为%

105.678 % 1 = 0.678
通过从原始数字中减去这个余数,我们将只剩下整数

105.678 - 0.678 = 105
要包含
x
小数位数,我们需要首先将原始数字乘以该小数位数的幂10,从而将小数点向后移动
x
位置。在本例中,我们将采用
x=2

105.678 * 10^2 
= 105.678 * 100
= 10567.8
现在,我们通过再次减去余数来重复相同的过程

10567.8 % 1 = 0.8
10567.8 - 0.8 = 10567
为了返回到要求的位置数,我们将其除以10^x

10567 / 10^2
= 10567 / 100
= 105.67
希望有帮助

如果不需要动态小数位数,则可以进一步简化

function truncateDec(num) {
    return (num*100 - num*100 % 1)/100;
}
alert(truncateDec(105.678)); // Returns 105.67

它是如何工作的?

其概念是,主截断通过将原始小数点除以1得到余数来工作。其余的将是小数点处的任何内容。余数运算符为%

105.678 % 1 = 0.678
通过从原始数字中减去这个余数,我们将只剩下整数

105.678 - 0.678 = 105
要包含
x
小数位数,我们首先需要将原始数字乘以该小数位数的幂10