我正在尝试转换数字数组,以使每个数字只有一个非零数字。 所以基本上
"7970521.5544"
会给我
["7000000", "900000", "70000", "500", "20", "1", ".5", ".05", ".004", ".0004"]
我尝试过:
var j = "7970521.5544"
var k =j.replace('.','')
var result = k.split('')
for (var i = 0; i < result.length; i++) {
console.log(parseFloat(Math.round(result[i] * 10000) /10).toFixed(10))
}
有什么想法,我不确定从这里去哪里?
答案 0 :(得分:4)
10
的相应幂,例如:
value = value * Math.pow(10, index); // for digits before decimal
value = value * Math.pow(10, -1 * index); // for digits after decimal
var n = "7970521.5544"
var arr = n.split('.'); // '7970521' and '5544'
var left = arr[0].split('').reverse(); // '1250797'
var right = arr[1].split(''); // '5544'
for(let i = 0; i < left.length; i++)
left[i] = (+left[i] * Math.pow(10, i) || '').toString();
for(let i = 0; i < right.length; i++)
right[i] = '.' + +right[i] * Math.pow(10, -i);
let res = left.reverse() // reverses the array
.filter(n => !!n)
// ^^^^^^ filters those value which are non zero
.concat(right.filter(n => n !== '.0'));
// ^^^^^^ concatenation
console.log(res);
答案 1 :(得分:3)
您可以将git push origin B:A
和Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range
Dim r As Long
If Not Intersect(Range("G:H"), Target) Is Nothing Then
Application.EnableEvents = False
Application.ScreenUpdating = False
For Each rng In Intersect(Range("G:H"), Target)
r = rng.Row
If Range("G" & r) = 5 And Range("H" & r) = "A" Then
Range("I" & r) = "M"
Range("I" & r).Interior.ColorIndex = 3
ElseIf Range("G" & r) = 5 And Range("H" & r) = "B" Then
Range("I" & r) = "H"
Range("I" & r).Interior.ColorIndex = 4
Else
Range("I" & r).Interior.ColorIndex = 0
End If
Next rng
End if
End Sub
与padStart
结合使用来构建数组。您要填充的数量将是小数点的索引减去循环中小数点左边的项和右边的相反项的索引。
使用padEnd
可以创建一个带有填充字符串的新数组,并注意避免零和小数本身。
reduce()
答案 2 :(得分:0)
您可以split
的字符串,然后使用Array.prototype.reduce
方法。记下小数位,然后相应地用"0"
填充值。如下所示:
var s = "7970521.5544";
var original = s.split('');
var decimalPosition = original.indexOf('.');
var placeValues = original.reduce((accum, el, idx) => {
var f = el;
if (idx < decimalPosition) {
for (let i = idx; i < (decimalPosition - 1); i++) {
f += "0";
}
accum.push(f);
} else if (idx > decimalPosition) {
let offset = Math.abs(decimalPosition - idx) - 2;
for (let i = 0; i <= offset; i++) {
f = "0" + f;
}
f = "." + f;
accum.push(f);
}
return accum;
}, []);
console.log(placeValues);
答案 3 :(得分:0)
更短的替代方法(不适用于IE):
var s = "7970521.5544"
var i = s.split('.')[0].length
var a = [...s].reduce((a, c) => (i && +c && a.push(i > 0 ?
c.padEnd(i, 0) : '.'.padEnd(-i, 0) + c), --i, a), [])
console.log( a )
IE版本:
var s = "7970521.5544"
var i = s.split('.')[0].length
var a = [].reduce.call(s, function(a, c) { return (i && +c && a.push(i > 0 ?
c + Array(i).join(0) : '.' + Array(-i).join(0) + c), --i, a); }, [])
console.log( a )
答案 4 :(得分:0)
function standardToExpanded(n) {
return String(String(Number(n))
.split(".")
.map(function(n, i) {
// digits, decimals..
var v = n.split("");
// reverse decimals..
v = i ? v.reverse() : v;
v = v
.map(function(x, j) {
// expanded term..
return Number([x, n.slice(j + 1).replace(/\d/g, 0)].join(""));
})
.filter(Boolean); // omit zero terms
// unreverse decimals..
v = i ? v.map(function(x) {
return '.' + String(x).split('').reverse().join('')
}).reverse() : v;
return v;
})).split(',');
}
console.log(standardToExpanded("7970521.5544"));
// -> ["7000000", "900000", "70000", "500", "20", "1", ".5", ".05", ".004", ".0004"]
这似乎是我儿子旧的三年级(核心课程)数学书中的内容!