Jen*_*lle 18 javascript number-formatting
我需要添加零,以便每个数字至少有两位小数,但没有舍入.例如:
5 --> 5.00
5.1 --> 5.10
5.11 --> 5.11 (no change)
5.111 --> 5.111 (no change)
5.1111 --> 5.1111 (no change)
Run Code Online (Sandbox Code Playgroud)
我的函数缺少一个IF来检查少于两个小数位:
function addZeroes( num ) {
var num = Number(num);
if ( //idk ) {
num = num.toFixed(2);
}
return num;
}
Run Code Online (Sandbox Code Playgroud)
谢谢!
除了以下两个之外,还会发布一个替代答案.(请记住,我不是专家,这只是用于文本输入,而不是用于解析复杂值,例如可能存在浮点问题的颜色等)
function addZeroes( value ) {
//set everything to at least two decimals; removs 3+ zero decimasl, keep non-zero decimals
var new_value = value*1; //removes trailing zeros
new_value = new_value+''; //casts it to string
pos = new_value.indexOf('.');
if (pos==-1) new_value = new_value + '.00';
else {
var integer = new_value.substring(0,pos);
var decimals = new_value.substring(pos+1);
while(decimals.length<2) decimals=decimals+'0';
new_value = integer+'.'+decimals;
}
return new_value;
}
Run Code Online (Sandbox Code Playgroud)
[这不是一个重复的问题.您链接的问题假设"知道他们至少有1个小数." 在文本输入中不能假设小数点,这就是错误.]
Fer*_*mux 27
干得好:
function addZeroes(num) {
// Convert input string to a number and store as a variable.
var value = Number(num);
// Split the input string into two arrays containing integers/decimals
var res = num.split(".");
// If there is no decimal point or only one decimal place found.
if(res.length == 1 || res[1].length < 3) {
// Set the number to two decimal places
value = value.toFixed(2);
}
// Return updated or original number.
return value;
}
// If you require the number as a string simply cast back as so
var num = String(value);
Run Code Online (Sandbox Code Playgroud)
请参阅更新的小提琴进行演示.
http://jsfiddle.net/jhKuk/159/
ran*_*ame 11
以下代码提供了一种方法来执行您想要的操作.还有其他人.
function addZeroes( num ) {
// Cast as number
var num = Number(num);
// If not a number, return 0
if (isNaN) {
return 0;
}
// If there is no decimal, or the decimal is less than 2 digits, toFixed
if (String(num).split(".").length < 2 || String(num).split(".")[1].length<=2 ){
num = num.toFixed(2);
}
// Return the number
return num;
}
Run Code Online (Sandbox Code Playgroud)
alert(addZeroes(5)); // Alerts 5.00
alert(addZeroes(5.1)); // Alerts 5.10
alert(addZeroes(5.11)); // Alerts 5.11
alert(addZeroes(5.111)); // Alerts 5.111
Run Code Online (Sandbox Code Playgroud)
也许使用.toLocaleString():
var num = 5.1;
var numWithZeroes = num.toLocaleString("en",{useGrouping: false,minimumFractionDigits: 2});
console.log(numWithZeroes);
Run Code Online (Sandbox Code Playgroud)
作为功能/演示:
var num = 5.1;
var numWithZeroes = num.toLocaleString("en",{useGrouping: false,minimumFractionDigits: 2});
console.log(numWithZeroes);
Run Code Online (Sandbox Code Playgroud)
而且,如果您必须使用.toFixed(),那么这里是单线:
var num = 5.1;
var numWithZeroes = num.toFixed(Math.max(((num+'').split(".")[1]||"").length, 2));
console.log(numWithZeroes);
Run Code Online (Sandbox Code Playgroud)
或者,再次作为功能/演示:
function addZeroes(num) {
return num.toLocaleString("en", {useGrouping: false, minimumFractionDigits: 2})
}
console.log('before after correct');
console.log('5 ->', addZeroes(5) , ' --> 5.00');
console.log('5.1 ->', addZeroes(5.1) , ' --> 5.10');
console.log('5.11 ->', addZeroes(5.11) , ' --> 5.11 (no change)');
console.log('5.111 ->', addZeroes(5.111) , ' --> 5.111 (no change)');
console.log('5.1111 ->', addZeroes(5.1111) , '--> 5.1111 (no change)');
console.log('-5 ->', addZeroes(-5) , ' --> -5.00');Run Code Online (Sandbox Code Playgroud)
小智 5
decimalNumber = number => Number.isInteger(number) ? number.toFixed(2) : numberRun Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
40111 次 |
| 最近记录: |