thousand seperator function

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 1500 Views - Last Post: 11 July 2012 - 12:44 PM

#1 Toxicterror  Icon User is offline

  • blub

Reputation: 15
  • View blog
  • Posts: 687
  • Joined: 10-March 09

thousand seperator function

Posted 10 July 2012 - 10:05 AM

Hi

Not telling much, because im just about to take this computer and throw it out if the window!

function format(number) {
    var s, i, rev;
    s = number.toString();
    s.split('');
    s.reverse();
    i = 3;
    while (i < number.length) {
        s.splice(i, 0, '.');
        i+=3;
    }
    s.reverse();
    s.join('');
    return s;
}


eg: number = 1124235 / number = -125123
what i try to do: number = 1.124.235 / number = -125.123

no need for decimals, only integer

I HATE Javascript!!!!!

cheers :(

This post has been edited by Toxicterror: 10 July 2012 - 10:06 AM


Is This A Good Question/Topic? 0
  • +

Replies To: thousand seperator function

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5676
  • View blog
  • Posts: 12,194
  • Joined: 02-June 10

Re: thousand seperator function

Posted 10 July 2012 - 03:04 PM

.

This post has been edited by tlhIn`toq: 11 July 2012 - 05:45 AM

Was This Post Helpful? 0
  • +
  • -

#3 Toxicterror  Icon User is offline

  • blub

Reputation: 15
  • View blog
  • Posts: 687
  • Joined: 10-March 09

Re: thousand seperator function

Posted 10 July 2012 - 03:56 PM

dont know what you mean...
what should i specify as int?
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3577
  • View blog
  • Posts: 10,442
  • Joined: 08-June 10

Re: thousand seperator function

Posted 10 July 2012 - 10:20 PM

@tlhIn`toq: there is no int in Javascript, only float. the best you could do is Number.toFixed() but I don’t think that’s the problem here.

the problem here arises in line #4, String.split() returns an array, it does not transform the String object into an Array object. so you would need something along var a = s.split('');

a look into the Error Console would have brought up the issue by showing that s.reverse() is undefined (which hints that s ain’t an array)

This post has been edited by Dormilich: 10 July 2012 - 10:28 PM

Was This Post Helpful? 2
  • +
  • -

#5 Toxicterror  Icon User is offline

  • blub

Reputation: 15
  • View blog
  • Posts: 687
  • Joined: 10-March 09

Re: thousand seperator function

Posted 11 July 2012 - 03:02 AM

worked thanks :)

next problem...
s.splice(i, 0, '.'); seems to be ignored because no . are added :/
Was This Post Helpful? 0
  • +
  • -

#6 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3577
  • View blog
  • Posts: 10,442
  • Joined: 08-June 10

Re: thousand seperator function

Posted 11 July 2012 - 03:52 AM

nope.

hint: what is the length of a number?
Was This Post Helpful? 0
  • +
  • -

#7 Toxicterror  Icon User is offline

  • blub

Reputation: 15
  • View blog
  • Posts: 687
  • Joined: 10-March 09

Re: thousand seperator function

Posted 11 July 2012 - 05:50 AM

dont know... same as it is with a string?
anyway, tried it with a.length and s.length but none of them worked either..



updated function:
function format(number) {
    var s, i = 3, a;
    s = number.toString();
    a = s.split('');
    a.reverse();
    while (i < s.length) {
        a.splice(i, 0, '.');
        i+=3;
    }
    a.reverse();
    s = a.join('');
    return s;
}

This post has been edited by Toxicterror: 11 July 2012 - 05:50 AM

Was This Post Helpful? 0
  • +
  • -

#8 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3577
  • View blog
  • Posts: 10,442
  • Joined: 08-June 10

Re: thousand seperator function

Posted 11 July 2012 - 07:01 AM

View PostToxicterror, on 11 July 2012 - 02:50 PM, said:

dont know... same as it is with a string?

the other way round, if I didnít know anything about numbers, how would you explain the length of a number to me?

on second thought, does the JS Number object own a length property?


View PostToxicterror, on 11 July 2012 - 02:50 PM, said:

anyway, tried it with a.length and s.length but none of them worked either..

1) why should you loop over the string length when you loop over the array?
2) worked for me --- kind of. (it did insert dots ...)
Was This Post Helpful? 1
  • +
  • -

#9 Toxicterror  Icon User is offline

  • blub

Reputation: 15
  • View blog
  • Posts: 687
  • Joined: 10-March 09

Re: thousand seperator function

Posted 11 July 2012 - 07:10 AM

JS and me.... trial and error style ;)

got it working
number comes as string, and on some line after this function i had a strange + in the middle of the code which probably caused that i never saw the dots :)

function format(number) {
    var i = 3, a;
    a = number.split('');
    a.reverse();
    while (i < number.length) {
        a.splice(i, 0, '\.');
        i+=4;
    }
    a.reverse();
    number = a.join('');
    return number;
}


thanks alot, but i still hate JS :D
Was This Post Helpful? 0
  • +
  • -

#10 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3577
  • View blog
  • Posts: 10,442
  • Joined: 08-June 10

Re: thousand seperator function

Posted 11 July 2012 - 07:23 AM

JS is cool once you get the hang of it (the biggest mistake is to treat it a derivative of C or Java).


and now some optimisation/compacting:
function format(number) {
    var i = 3, a = number.split('').reverse();
    while (i < number.length) {
        a.splice(i, 0, '\.');
        i += 4;
    }
    return a.reverse().join('');
}

hm, why not splitting it in groups of 3 and join with a dot?
Was This Post Helpful? 0
  • +
  • -

#11 Toxicterror  Icon User is offline

  • blub

Reputation: 15
  • View blog
  • Posts: 687
  • Joined: 10-March 09

Re: thousand seperator function

Posted 11 July 2012 - 07:46 AM

dont know.. saw alot of ways how people did it.. regext etc., but none of them worked for me :/

how many .xyz() can you do in one "term"?
is it possible to call the same method a few times on one line?

This post has been edited by Toxicterror: 11 July 2012 - 07:56 AM

Was This Post Helpful? 0
  • +
  • -

#12 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5932
  • View blog
  • Posts: 12,855
  • Joined: 16-October 07

Re: thousand seperator function

Posted 11 July 2012 - 08:59 AM

Hmm... either I'm missing something here or you guys are. Code yields 1124.235 for 1124235. Is this intended?

Anyway, just for fun:
function format(number) {
	var result = '';
	var a = number.toString().split('');
	var ct = (a.length % 3) + 1;
	for(var i=0; i<a.length; i++) {
		if (--ct < 1) {
			if (i!=0) { result += '.'; }
			ct = 3;
		}
		result += a[i];
	}
    return result;
}



This yeilds 1.234.567 for 1234567.

Here's my little test script, since it might be helpful:
<html>
<body>
<ol>
<script type="text/javascript">
function format1(number) {
	number = number.toString();
    var i = 3, a;
    a = number.split('');
    a.reverse();
    while (i < number.length) {
        a.splice(i, 0, '\.');
        i+=4;
    }
    a.reverse();
    number = a.join('');
    return number;
}

function format2(number) {
	number = number.toString();
    var i = 3, a = number.split('').reverse();
    while (i < number.length) {
        a.splice(i, 0, '\.');
        i += 4;
    }
    return a.reverse().join('');
}


function format3(number) {
	var result = '';
	var a = number.toString().split('');
	var ct = (a.length % 3) + 1;
	for(var i=0; i<a.length; i++) {
		if (--ct < 1) {
			if (i!=0) { result += '.'; }
			ct = 3;
		}
		result += a[i];
	}
    return result;
}

function write(s) { document.write("<li>" + s + "</li>"); }

function tstFunc(f) { 
	var n = 1;
	for(var i=1; i<10; i++, n = n*10 + i) {
		write(n + " = '" + f(n) + "'");	
	}
}

function testAll() {
	var tests = [ ['format1', format1], ['format2', format2], ['format3', format3] ];
	for(var i in tests) {
		write("--- " + tests[i][0]);
		tstFunc(tests[i][1]);
		write("---------");
	}
}

testAll();
write("Done");
</script>
</ol>
</body>
</html>



Results:
--- format1
1 = '1'
12 = '12'
123 = '123'
1234 = '1.234'
12345 = '12.345'
123456 = '123.456'
1234567 = '1234.567'
12345678 = '12.345.678'
123456789 = '123.456.789'
---------
--- format2
1 = '1'
12 = '12'
123 = '123'
1234 = '1.234'
12345 = '12.345'
123456 = '123.456'
1234567 = '1234.567'
12345678 = '12.345.678'
123456789 = '123.456.789'
---------
--- format3
1 = '1'
12 = '12'
123 = '123'
1234 = '1.234'
12345 = '12.345'
123456 = '123.456'
1234567 = '1.234.567'
12345678 = '12.345.678'
123456789 = '123.456.789'
---------


Was This Post Helpful? 0
  • +
  • -

#13 Toxicterror  Icon User is offline

  • blub

Reputation: 15
  • View blog
  • Posts: 687
  • Joined: 10-March 09

Re: thousand seperator function

Posted 11 July 2012 - 10:01 AM

noticed that too but couldnt be bothered to fix it :)
may i use your code snippet and give credit to you? :)
Was This Post Helpful? 0
  • +
  • -

#14 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5932
  • View blog
  • Posts: 12,855
  • Joined: 16-October 07

Re: thousand seperator function

Posted 11 July 2012 - 10:12 AM

The code is right there. Use it as you like. ;)

I'm concerned with the "couldn't bothered to fix it" thing, though. You should always be bothered! Having one case fail like that would make me mental. :P
Was This Post Helpful? 0
  • +
  • -

#15 Toxicterror  Icon User is offline

  • blub

Reputation: 15
  • View blog
  • Posts: 687
  • Joined: 10-March 09

Re: thousand seperator function

Posted 11 July 2012 - 10:16 AM

was trying to fix just how the number is formated for to long now ^^
it was/is working to 90%... got fed up with it :D

and the case where the number would be bigger than 123.123 is veeery small :D
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2