Suppose we have the number 12345678, we need to split it into an array of several numbers with 3 characters each starting from the end, if the number of characters in our number is not a multiple of 3, then the first element of the array should be shorter, that is, the result should it will turn out 12,345,678.
- oo, very much helped out, thank you so much! he also solved his problem, but I got a bike at all) - megam
- That you have not yet met with "really difficult tasks" =) This task is simple. All "non-smoothness" consists only in the fact that the number of digits may not be a multiple of 3. - Salivan
|
6 answers
The most concise option :-)
s="1234567890"; result= s.split( /(?=(?:\d{3})+(?!\d))/ ); // [1, 234, 567, 890]
- oneO___o probably never stop wondering what can be done with RegExp`s - Specter
- oneYou can simplify a little:
/(?=(?:\d{3})+$)/
- Ilya Pirogov - Nichrome, just two lines! =) - Salivan
- @Salivan if you do not create a global variable, then you can do without it var result = '1234567890'.split (/ (? = (?: \ D {3}) + $) /); // [1, 234, 567, 890] - higimo
|
I heard unshift
not all implementations of js, it works fast, but it’s a simple concise solution:
var a = 12345678..toString().split(''), result = [], start; while((start = a.length - 3) > 0){ result.unshift(a.splice(start, 3)); } result.unshift(a); console.log(result); // [[1, 2], [3, 4, 5], [6, 7, 8]]
you need to break it into an array of several numbers of 3 characters
misunderstood the question, here is a better solution:
var a = 12345678..toString(), result = [], start = a.length, end; while((end = start) > 0){ start = end - 3; result.unshift(~~a.substring(start, end)); // result = [~~a.substring(start, end)].concat(result); // возможно быстрее } console.log(result); // [12, 345, 678]
|
And one more look at things:
function divide(number, digits) { var result = []; var interm = []; var counter = 1; while(number >= 1) { mod = number % 10; number = number / 10; interm.unshift(parseInt(mod)); if(!(counter % digits)) { result.unshift(interm); interm = []; } counter++; } result.unshift(interm); console.log(result.join('\n')); }
There is also little elegant, although I have not tried myself in JavaScript.
|
And another option :)
function triades(n){ var x=parseInt(n,10).toString(); var r=/(\d+)(\d{3})/; while(r.test(x)){ x=x.replace(r,'$1,$2'); }; return x.split(','); }
|
So I also decided to write a special function:
(This is, of course, not as elegant as the @Spectre option, but still, the logic of the work is completely different).
function simple_splitter(dig) { var array = [], mask = dig.toString(), key = (mask.length)%3, first_sp = (key==0) ? 3:key, i = 0; while(mask.length>3) { var sub_str, k; k = (i==0) ? first_sp:3; sub_str = mask.substr(0,k); array.push(sub_str); mask = mask.substr(k,mask.length-k); if(mask.length == 3) {array.push(mask); break;} i++; } return array; }
If you throw out all the excess, the code will be a little shorter. Application:
var virtual_digit = 1234567; arr = new Array(); arr = simple_splitter(virtual_digit); alert(arr.toString()); // [1,234,567]
- 2a couple of notes: - use Single var Pattern - use literals instead of constructors: - Primitive Wrappers - Array literal - Specter
- Thank you, @Spectre! Fully agree that I varied the "vars". I also agree with the used array and string constructors - they are obviously superfluous here. But I used them intentionally, for greater clarity. - Salivan
|
From one project:
function fixNumber(n){ var s = n.toString().split('.'); var r = ''; for(var i = s[0].length-3; i > 0; i-=3) r = s[0].substr(i, 3) + ' ' + r; r = s[0].substr(0, i+3) + ' ' + r; r = r.substr(0, r.length-1); s[1] = s[1] || '00'; return r+'.'+s[1]; }
|