SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Evangelist Maujor's Avatar
    Join Date
    Mar 2005
    Location
    Rio de Janeiro - Brazil - South America
    Posts
    409
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Convert number to decimal

    Is there a JavaScript method to convert a "plain" number into a decimal one?
    Say: 12345678 is converted to 12.345.678
    Mauricio Samy Silva
    http://www.maujor.com/

  2. #2
    SitePoint Addict Smola's Avatar
    Join Date
    Mar 2005
    Posts
    260
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That isn't a valid decimal...

    Is there any pattern to your intent? Or you just want to move the decimal place from the back of the integer to somewhere inside or in front of the number?
    Humbly,

    Smola

  3. #3
    SitePoint Evangelist Maujor's Avatar
    Join Date
    Mar 2005
    Location
    Rio de Janeiro - Brazil - South America
    Posts
    409
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Smola,
    Thanks for reply.
    There isn't a pattern to apply a regexp.
    I need a generic method to make the conversion:

    nnnnnnnnnnn... => n.nnn.nnn.nnn.nnn....

    Edited: Well, it seems a pattern. Split from right to left step 3!
    Mauricio Samy Silva
    http://www.maujor.com/

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,789
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Presumably you are using the . as a thousands separateor but have confused everyone by referring to decimals (where presumably you use a comma but most people reading expect them the other way around).

    Take a look at http://javascript.about.com/library/blnumfmt.htm
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  5. #5
    SitePoint Evangelist Maujor's Avatar
    Join Date
    Mar 2005
    Location
    Rio de Janeiro - Brazil - South America
    Posts
    409
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    [solved]

    Quote Originally Posted by felgall View Post
    ...have confused everyone by referring to decimals...
    Yes, my fault. Very sorry.
    And confused the search engine also :-)
    Stephen I know your work and I have already made a search for one of yours scripts at about.com - with the wrong keyword.

    And as ever, your script works like a charm.
    Mauricio Samy Silva
    http://www.maujor.com/

  6. #6
    SitePoint Addict Smola's Avatar
    Join Date
    Mar 2005
    Posts
    260
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well Maujor I don't think there is a single method for it but I went ahead and wrote a block of code that does the trick. You can even customize how many numbers you want between each decimal.

    Code javascript:
    function reverseChars(theString) {		//reverse a string character by character
    	var revStr = "";
    	for(var i = theString.length - 1; i > -1; i--) {
    		revStr += theString.charAt(i);
    	};
    	return revStr;
    };
     
    function dot(reversedString, chunks) {  //chunks is how many #'s in between .'s
    	var dotStr = "";
    	for (var j = 0; j < reversedString.length; j++) {
    		if (j > 0 && j &#37; chunks == 0) {
    			dotStr += "."+ reversedString.charAt(j);
    		} else {
    			dotStr += reversedString.charAt(j);
    		};
    	};
    	return dotStr;
    };
     
    var num = 1234567890;
    var numStr = num.toString();
     
    s1 = reverseChars(numStr);	//reverse number
    s2 = dot(s1,2);				//put decimals at desired intervals
    result = reverseChars(s2);	//reverse it again
    //result is your final result!

    Now, your result is a string because no number has the format you wish. If you wanted to be bale to use each chunk of numbers then a different approach would need to be taken with arrays.

    Hope this helps...


    EDIT: Well fellas it looks like you made nice already. I didn't check for updates to the thread while I was toying with the solution above. It will still work with commas, just change the . to a , and voila!
    Last edited by Smola; May 2, 2009 at 15:12. Reason: Conversation as I was writing a solution
    Humbly,

    Smola

  7. #7
    SitePoint Evangelist Maujor's Avatar
    Join Date
    Mar 2005
    Location
    Rio de Janeiro - Brazil - South America
    Posts
    409
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Great Smola!
    Your script works like a charm.
    Now let's go to Firebug and check the run time (speed) for the two scripts.
    Thanks a lot
    Mauricio Samy Silva
    http://www.maujor.com/


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •