我有一些价值观:
var one = 1.0000
var two = 1.1000
var three = 1.1200
var four = 1.1230
Run Code Online (Sandbox Code Playgroud)
和功能:
function tofixed(val)
{
return val.toFixed(2);
}
Run Code Online (Sandbox Code Playgroud)
这个回报:
1.00
1.10
1.12
1.12
Run Code Online (Sandbox Code Playgroud)
我希望dot-2之后的最大尺寸,但只有在!之后的数字为0. = 0.所以我希望收到:
1
1.1
1.12
1.12
Run Code Online (Sandbox Code Playgroud)
我该怎么做?
预期结果:
(1.175).toFixed(2) = 1.18 and
(5.175).toFixed(2) = 5.18
Run Code Online (Sandbox Code Playgroud)
但在JS中显示:
(1.175).toFixed(2) = 1.18 but
*(5.175).toFixed(2) = 5.17*
Run Code Online (Sandbox Code Playgroud)
如何纠正这个问题?
我对html,javascript和css很新,所以请原谅我的问题听起来很愚蠢.我的问题是如何防止函数toFixed()舍入十进制数.
这是我的链接:http://jsfiddle.net/RWBaA/4/
我正在尝试做的是,只要用户在文本框中输入,我就会检查输入是否为有效的十进制数.同时我还想检查输入是否是有效货币,这意味着它只能在小数点右侧再添加两个数字.问题是当用户在小数点后输入第3个数字时,如果第3个数字> = 5,则小数点后的第2个数字四舍五入到最接近的百分数.
测试输入:
Input Output 123456.781 -> 123456.78 123456.786 -> 123456.79
为什么我的代码不允许使用chrome中的箭头键?
请帮忙.如果您有更好的解决方案,您可以自由建议.提前致谢.
distanceX = ((this.frontclickedX - originX) / originX).toFixed(2);
distanceY = (-(this.frontclickedY - originY) / originY).toFixed(2);
let distanceZ: number = (-(this.frontclickedY - originY) / originY).toFixed(2);
Run Code Online (Sandbox Code Playgroud)
当我计算distanceZ时,我的浏览器抛出错误:
Type 'string' is not assignable to type 'number'.
Run Code Online (Sandbox Code Playgroud)
前两行完美运行,但第三行并不真正有效.正如您所看到的,它甚至与distanceY完全相同.我试图首先解析Flos().但后来它开始抱怨我只能解析一个字符串到一个浮点数,而我没有提供一个.
我现在很困惑,所以一些帮助或解决方案将非常感激!
提前致谢!
编辑:我试图简化我的代码.使距离变得私密,并增加了二传手和吸气剂.
private _distanceX: number;
private _distanceY: number;
private _distanceZ: number;
get distanceZ(): number {
return this._distanceZ;
}
set distanceZ(value: number) {
this._distanceZ = value;
} ...
calculateBadgeDistance(): void{
this.distanceX = (5.001).toFixed(2);
this.distanceY = (5.001).toFixed(2);
this.distanceZ = (5.001).toFixed(2);
}
Run Code Online (Sandbox Code Playgroud)
现在我得到了所有3行的错误.
我正在使用另一个我无法控制且看不到的代码库。我可以向它发送输入,它会将输出返回给我。这个代码库也没有指定它是用什么写的,但这并不重要,因为我只需要传递它的数字,它就会把它的输出数字交给我。我只知道预期的输入和输出是什么。
我必须处理的输入之一要求我提供一个长度正好为 6 位小数的输入。我的大多数数字都远高于 6 位小数,所以我可以四舍五入或截断它,但如果它被四舍五入或随机恰好是一个像0.125or的数字0.5,我无法输入正确的数字。
例如,如果我有一个像 那样的数字0.5,那是一个有效且合法的数字,但是我输入它的函数不会接受它并且会出错,因为它不是像0.500000is一样精确到 6 位小数的数字。我也无法输入 的字符串"0.500000",因为它也会出错,因为它正在寻找一个数字,而不是一个字符串。
无论如何,即使在数字末尾有额外的死零,本机 JavaScript 是否也能保持数字的特定精度?
例子:
(1/2).toFixed(6) === "0.500000" // the toFixed() function returns a *String*, not a *Number*.
(1/2).toFixed(6) /1 === 0.5 // dividing by 1 coerces it to a Number, but drops the decimals back off the end.
Number( (1/2).toFixed(6) ) === 0.5 // Trying to convert it into a Number using the Number Constructor does the same as …Run Code Online (Sandbox Code Playgroud) 我注意到在调用toFixed负指数数字时,结果是一个数字,而不是一个字符串.
首先,我们来看看规格.
Number.prototype.toFixed (fractionDigits)返回
String包含此十进制定点表示法的数字值,小数点后面的fractionDigits数字.如果是fractionDigitsundefined,0则假设.
实际发生的是(在Chrome,Firefox,Node.js中测试):
> -3e5.toFixed()
-300000
Run Code Online (Sandbox Code Playgroud)
所以,返回的值是-3e5.另外,请注意这不是一个字符串.这是一个数字:
> x = -3e5.toFixed()
-300000
> typeof x
'number'
Run Code Online (Sandbox Code Playgroud)
如果我将输入包装在括号中,它按预期工作:
> x = (-3e5).toFixed()
'-300000'
> typeof x
'string'
Run Code Online (Sandbox Code Playgroud)
为什么会这样?解释是什么?
我正在尝试设置一个mustache.js模板,使用lambda将数字格式化为特定的小数位,我遇到了问题.给定一个看起来像这样的对象:
{
x: 123,
points: [
{ name: "foo", y: 1.234567 },
{ name: "bar", y: 2.123456 },
{ name: "fax", y: 3.623415 }
]
}
Run Code Online (Sandbox Code Playgroud)
首先,我尝试设置一个模板,看起来像:
var template = "{{x}}{{#points}}<br/>{{name}}, {{#y.toFixed}}2{{/y.toFixed}}";
Run Code Online (Sandbox Code Playgroud)
这不起作用(生成一个空的空间,数字应该是.我可能lambda不是正确的格式,因为toFixed不返回一个函数(胡子文档).所以我尝试:
Number.prototype.toMustacheFixed = function(){
var n = this;
return function(d){ return n.toFixed(d); };
};
var template = "{{x}}{{#points}}<br/>{{name}}, {{#y.toMustacheFixed}}2{{/y.toMustacheFixed}}"
Run Code Online (Sandbox Code Playgroud)
再次,失败.我甚至尝试将toMustacheFixed函数简化为:
Number.prototype.toMustacheFixed = function(){
return function(){ return 123.45; };
};
Run Code Online (Sandbox Code Playgroud)
这没有用.模板中我还是一片空白.那么,Mustache.js可以不处理数字上的原生函数和原型函数,还是我做错了什么?
我只是想整理一下1.275.toFixed(2),我期待的回报是1.28而不是1.27.
使用各种计算器和舍入到最接近的百分之一的简单方法,如果最后一个数字大于或等于五,它应该向上舍入.
如果这不能与toFixed(2)一起使用,那怎么办?
人们询问console.log(1.275.toFixed(2))是否打印掉1.28,这是一个快速截图MacOS Chrome版本55.0.2883.95(64位)
我有一个数字输入字段,它在用户键入时通过状态设置新值。这工作正常。但是,如果我向其添加小数(toFixed),则在仅输入一位数字后,光标将跳转到输入的末尾。例如,当我删除现有内容并键入其他内容时,就会发生这种情况。如果我在第一个和小数点之间添加一个数字,则光标不会跳转。有没有人以前遇到过这种情况,解决方案是什么?
小提琴显示问题:https : //jsfiddle.net/Inverness/k04yvq1u/
constructor() {
super()
this.state = {
number: 1
}
this.handleInputChange = this.handleInputChange.bind(this)
}
handleInputChange(event) {
console.log(event.target.value)
this.setState({
number: event.target.value
})
}
render() {
return (
<div>
<input
type="number"
value={ parseFloat(this.state.number).toFixed(2) }
onChange={ this.handleInputChange }
/>
</div>
)
}
Run Code Online (Sandbox Code Playgroud) 我有一个用dat.guiJavaScript 库制作的小菜单。我使用不同的行显示一些初始值(这些初始值在代码执行过程中被修改)。
我的问题是,例如,15我想显示“15.0000”而不是显示值“ ”。我尝试使用toFixed(4)功能但没有成功。
这是原始(没有“ toFixed(4)”函数)代码片段:
var componentThetaInit = 15;
var componentPhiInit = 15;
var gui = new dat.GUI({
autoplace: false,
width: 350,
height: 9 * 32 - 1
});
var params = {
StartingVector : '',
ComponentVectorTheta : componentThetaInit,
ComponentVectorPhi : componentPhiInit
};
gui.add(params, 'StartingVector').name('Starting Vector :');
controllerComponentVectorTheta = gui.add(params, 'ComponentVectorTheta', minComponentTheta, maxComponentTheta, 0.0001).name('Component θ ');
controllerComponentVectorPhi = gui.add(params, 'ComponentVectorPhi', minComponentPhi, maxComponentPhi, 0.0001).name('Component φ ');
Run Code Online (Sandbox Code Playgroud)
现在我试过:
var params = {
StartingVector : …Run Code Online (Sandbox Code Playgroud)