I want a function that can take a number (say 1000.43) and return the tens, hundreds, thousands, and decimal part. So in 1000.43, it would maybe return an array with [1000, 0, 0, .43]
. I know it sounds hard but how would I do that. Maybe you can give me some advice on how to do that or can supply me with the code. Either way is greatly appreciated.
I want a function that can take a number (say 1000.43) and return the tens, hundreds, thousands, and decimal part. So in 1000.43, it would maybe return an array with [1000, 0, 0, .43]
. I know it sounds hard but how would I do that. Maybe you can give me some advice on how to do that or can supply me with the code. Either way is greatly appreciated.
-
1
For 1000.43, wouldn't the array be
[1000, 0, 0, .43]
? And what about the one's place? – FishBasketGordo Commented Feb 25, 2012 at 16:31 - what would be the 1234.234 be - do you need to split the decimal part into tenth/hundriedth/thousandths? Would it be [1000, 200, 30, 4, .234] ? – Roman Goyenko Commented Feb 25, 2012 at 16:34
-
Actually, it would be
[1,0,0,.43]
. – Abhranil Das Commented Feb 25, 2012 at 16:35 - Related: stackoverflow./questions/3179783/php-leftmost-digit – Incognito Commented Feb 25, 2012 at 16:39
5 Answers
Reset to default 4Start with a string to get a consistent picture of your number without further worrying about floating point, which is more plicated than it sounds:
const toDecimal = (n) => {
if (!Number.isFinite(n)) {
throw new RangeError("number must be finite");
}
// ToString(n) may be of the form …e±…
let [significand, exponent] = String(n).split("e");
if (exponent === undefined) {
return significand.includes(".")
? significand
: significand + ".0";
}
const [integerPart, fractionalPart = ""] = significand.split(".");
const integerDigits = [...integerPart];
const fractionalDigits = [...fractionalPart].reverse();
exponent = Number(exponent);
while (exponent > 0) {
integerDigits.push(fractionalDigits.pop() ?? "0");
exponent--;
}
while (exponent < 0) {
fractionalDigits.push(integerDigits.pop() ?? "0");
exponent++;
}
return `${integerDigits.join("") || "0"}.${fractionalDigits.reverse().join("") || "0"}`;
};
(If you are starting or can start with a decimal string of the right form, you should skip this toDecimal
step and just use the string directly.)
After that, the value of each digit of the integer part can be determined by its position relative to the decimal point (i.e. the end of the integer part).
const toDigitsAndFractional = (n) => {
const [integerPart, fractionalPart] = toDecimal(n).split(".");
const result = Array.from(integerPart,
(c, i) => 10 ** (integerPart.length - i - 1) * Number(c));
result.push("." + fractionalPart);
return result;
};
const toDecimal = (n) => {
if (!Number.isFinite(n)) {
throw new RangeError("number must be finite");
}
// ToString(n) may be of the form …e±…
let [significand, exponent] = String(n).split("e");
if (exponent === undefined) {
return significand.includes(".")
? significand
: significand + ".0";
}
const [integerPart, fractionalPart = ""] = significand.split(".");
const integerDigits = [...integerPart];
const fractionalDigits = [...fractionalPart].reverse();
exponent = Number(exponent);
while (exponent > 0) {
integerDigits.push(fractionalDigits.pop() ?? "0");
exponent--;
}
while (exponent < 0) {
fractionalDigits.push(integerDigits.pop() ?? "0");
exponent++;
}
return `${integerDigits.join("") || "0"}.${fractionalDigits.reverse().join("") || "0"}`;
};
const toDigitsAndFractional = (n) => {
const [integerPart, fractionalPart] = toDecimal(n).split(".");
const result = Array.from(integerPart,
(c, i) => 10 ** (integerPart.length - i - 1) * Number(c));
result.push("." + fractionalPart);
return result;
};
console.log(JSON.stringify(toDigitsAndFractional(1000.43)));
console.log(JSON.stringify(toDigitsAndFractional(4314.23)));
console.log(JSON.stringify(toDigitsAndFractional(1000.43e+20)));
console.log(JSON.stringify(toDigitsAndFractional(1000.43e-20)));
.as-console-row-code {
word-break: break-word;
}
You could use Modulo (%) like this
var num = 1573.64;
var th = Math.floor((num % 10000) / 1000),
h = Math.floor((num % 1000) / 100),
t = Math.floor((num % 100) / 10),
u = Math.floor(num % 10),
d = num % 1;
console.log(th, h, t, u, d);
Writing as a function it could be something like
function splitNumber(num) {
var len = num.toString().split('.')[0].length, //get length above decimal point
d = Math.pow(10, len),
a = [];
for (var i = 0; i < len+1; i++) {
var x = num % d;
if (d > 1) {
d /= 10;
a.push(Math.floor(x / d));
} else {
//below decimal point
a.push(x);
}
}
return a;
}
This is the shortest yet plete implementation:
function parts(d) {
var num = (d+'').split('.')
, array = Array.prototype.slice.call(num[0])
, zeros = '';
for (var i = array.length-2; 0 <= i; i--) {
zeros += '0';
if (array[i] !== '0') array[i] = array[i] + zeros;
}
if (num[1]) array.push('.' + num[1]);
return array;
}
// Example:
parts(10.4); // [10, 0, .4]
First check for a .
and split the string on that. If their is a decimal add it to the array. Then loop backwards adding each number to the beginning of the array using unshift
Also in your loop if you have your counter starting at zero then that number is how many zeroes you will have to add to number. So some sample code.
var num = '1234.5';
var arr=[];
num = num.split('.');
if(num[1]!==''){
arr.unshift(num[1]);
}
part=num[0],len=part.length,zeroes='000000000000';
for(var i=0;i<len;i++){
var digit = part.substr(len-i-1,1);
arr.unshift(digit+zeroes.slice(0,i));
}
It ought to be simple- how many units, tens, hundreds, thousands...
just watch for decimals floating off on you.
function tithe(n){
if(!isFinite(n)) return NaN;
n= Math.abs(n);
var A= [], I= Math.floor(n),
dec= String(n).split('.')[1],
x= 1, t;
while(I/x> 0){
t= I%(x*= 10);
I-= t;
A.unshift(t);
}
if(dec) A.push(+dec);
return A;
}
tithe(12022045.554)
/* returned value: (Array)
10000000,2000000,0,20000,2000,0,40,5,554
*/
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745222767a4617325.html
评论列表(0条)