Javascript 小数点后四舍五入各不相同
我觉得小数点后四舍五入有些奇怪。 我使用+cast将.toFixed()返回的字符串转换为数字。 为了清晰起见,我做了一些测试并添加了一些数字。我对Javascript 小数点后四舍五入各不相同,javascript,rounding,Javascript,Rounding,我觉得小数点后四舍五入有些奇怪。 我使用+cast将.toFixed()返回的字符串转换为数字。 为了清晰起见,我做了一些测试并添加了一些数字。我对测试1和测试3 第一个测试按预期进行-预计小数点后两位四舍五入 然而,第三个在我看来很奇怪,它不应该也凑到123.4600 所有测试均通过 测验 模块 var yootil={}; yootil=(函数(模块){ 模块.实用程序={ /** *转换数字的字符串表示形式 *到小数点后的数字。 * * *var yo=yootil.Utilitie
测试1
和测试3
第一个测试按预期进行-预计小数点后两位四舍五入
然而,第三个在我看来很奇怪,它不应该也凑到123.4600
所有测试均通过
测验 模块
var yootil={};
yootil=(函数(模块){
模块.实用程序={
/**
*转换数字的字符串表示形式
*到小数点后的数字。
*
*
*var yo=yootil.Utilities;//创建缓存版本
*
*yo.stringToNumber()//null
*yo.stringToNumber(“12.3849”)//12.38
*yo.stringToNumber(“123.3849”,0)//123
*yo.stringToNumber(“test”)//null
*
*
*@param{string}string一个数字字符串
*@param{int}小数位
*@return{boolean}
**/
StringTonNumber:函数(字符串,小数点){
试一试{
如果(!string | | isNaN(+(string))){
抛出新错误('yootil.Utilities.StringTonNumber需要一个有效的数字字符串“123.45”);
}
小数位=(小数位===0)?0:(小数位)| 2;
return+(parseFloat(string).toFixed(小数位));
}catch(e){console.error(e);console.log(string);返回null;}
}
}
返回模块;
}(yootil=yootil |{});
exports.yootil=yootil;
在测试3中,您将一个数字四舍五入到四位小数——我在这里看到的所有内容都应该通过。你是说第二次考试吗?嘿,科里。我假设测试3
会转到123.4600
?@Cᴏʀʏ测试2也应该通过,就像没有给出小数位
参数一样,因为| 2
位,它变成2
。测试3为小数位
参数传递4
,所以它四舍五入到小数位4,而不是2。@Philip是的,但是,如果将123.4587
作为一个数字,并将4
小数位作为参数,那么它为什么要四舍五入到.4600
?如果您对字符串执行parseFloat(“123.4587”)
,您将得到123.4587
作为float,那么.toFixed(4)
除了将其转换回字符串“123.4587”
,而+“123.4587”
将返回相同的浮点数外,不会对其执行任何操作。我不认为这里会出什么问题。
var module = require("../../tests/yootil/Utilities.js");
// yootil.Utilities.stringToNumber
//
describe('yootil.Utilities.stringToNumber', function(){
// TEST 1
it('Should return a number with 2 decimals', function(){
expect(module.yootil.Utilities.stringToNumber("123.4585")).toEqual(123.46);
});
// TEST 2
it('Should return a number with zero decimal places', function(){
expect(module.yootil.Utilities.stringToNumber("123.4585", 0)).toEqual(123);
});
// TEST 3
it('Should return a number with 4 decimals', function(){
expect(module.yootil.Utilities.stringToNumber("123.4587", 4)).toEqual(123.4587);
});
// TEST 4
it('Should return null with an invalid string', function(){
expect(module.yootil.Utilities.stringToNumber("test")).toEqual(null);
});
// TEST 5
it('Should return null with NO parameters', function(){
expect(module.yootil.Utilities.stringToNumber()).toEqual(null);
});
});
var yootil = {};
yootil = (function(module){
module.Utilities = {
/**
* Converts a string representation of a number
* to a number with decimal places.
*
* <example>
* var yo = yootil.Utilities; // create a cached version
*
* yo.stringToNumber() // null
* yo.stringToNumber("12.3849") // 12.38
* yo.stringToNumber("123.3849", 0) // 123
* yo.stringToNumber("test") // null
* </example>
*
* @param {string} string A number string
* @param {int} Decimal places
* @return {boolean}
**/
stringToNumber: function(string, decimal_places){
try{
if(!string || isNaN(+(string))){
throw new Error('yootil.Utilities.stringToNumber expects a valid number string "123.45" )');
}
decimal_places = (decimal_places === 0) ? 0 : (decimal_places) || 2;
return +(parseFloat(string).toFixed(decimal_places));
}catch(e){ console.error(e); console.log(string); return null;}
}
}
return module;
}(yootil = yootil || {}));
exports.yootil = yootil;