Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Page 1 of 2 1 2 LastLast
Results 1 to 10 of 15
  1. #1
    Member dinologic's Avatar
    Join Date
    Jan 2003
    Posts
    54
    Member #
    539
    Is there a way to get 7200.7 to look like 7,200.70 in javascript without jumping thru hoops?

    Thanks.

  2.  

  3. #2
    WDF Staff smoseley's Avatar
    Join Date
    Mar 2003
    Location
    Boston, MA
    Posts
    9,729
    Member #
    819
    Liked
    205 times
    Unfortunately no.

    You can create a "pad" function relatively simply, though that does something like this:
    Code:
    function padNumber (number, decimalPlaces) {
        var numberString = (number.indexOf('.') >= 0) ? number : number + '.';
        var pad = decimalPlaces + numberString.length -  numberString.indexOf('.') - 1;
        for (var i = 0; i < pad; i++) {
            numberString += '0';
        }
        return numberString;
    }
    haven't tested it, but it should work

  4. #3
    Member dinologic's Avatar
    Join Date
    Jan 2003
    Posts
    54
    Member #
    539
    I didn't think there was an actual function...I was looking for something like that. Thanks!

    I am getting an error though in this line...
    Code:
    var numberString = (number.indexOf('.') >= 0) ? number : number + '.';
    Is that correct? I'm unfamiliar with the syntax. What is it supposed to do?

  5. #4
    Member dinologic's Avatar
    Join Date
    Jan 2003
    Posts
    54
    Member #
    539
    btw, I'm getting "object does not support this property or method"

    Thx

  6. #5
    WDF Staff smoseley's Avatar
    Join Date
    Mar 2003
    Location
    Boston, MA
    Posts
    9,729
    Member #
    819
    Liked
    205 times
    oh. it's because i was calling a String method on an int.

    Try adding this line before it:
    Code:
    number = '' + number;

  7. #6
    WDF Staff smoseley's Avatar
    Join Date
    Mar 2003
    Location
    Boston, MA
    Posts
    9,729
    Member #
    819
    Liked
    205 times
    Incidentally, the following syntax:
    Code:
    (condition) ? valueA : valueB
    basically says that if the condition is met, return valueA, otherwise return valueB. It can be used in many cases where an if...then...else statement wouldn't be feasible, such as a function call. Here's an example:
    Code:
    window.alert( (userIsAJerk) ? 'You are a jerk' : 'You are cool' );
    It makes things more compact

  8. #7
    Senior Member filburt1's Avatar
    Join Date
    Jul 2002
    Location
    Maryland, US
    Posts
    11,774
    Member #
    3
    Liked
    21 times
    You gotta love PHP:
    PHP Code:
    $something number_format($something); 
    filburt1, Web Design Forums.net founder
    Site of the Month contest: submit your site or vote for the winner!

  9. #8
    WDF Staff smoseley's Avatar
    Join Date
    Mar 2003
    Location
    Boston, MA
    Posts
    9,729
    Member #
    819
    Liked
    205 times
    LOL, PHP is like:
    PHP Code:
    $value function_i_never_thought_i_would_need($arg); 

  10. #9
    Member dinologic's Avatar
    Join Date
    Jan 2003
    Posts
    54
    Member #
    539
    Cool. Got it.

    Thanks for the shortcut as well. I have seen that before but never used it...will use it now though!

  11. #10
    Member dinologic's Avatar
    Join Date
    Jan 2003
    Posts
    54
    Member #
    539
    Just for kicks...here's what I ended up with...
    Code:
    function padNumber(number, decimal_required)
    {                                       
        	number = '' + number;
        	var number_string = (number.indexOf('.') >= 0) ? number : number + '.';
    	var decimal_position = number_string.indexOf('.') + 1;
    	var decimals_existing = number_string.length - decimal_position;
    	var pad = decimal_required - decimals_existing
        	for (var i = 0; i < pad; i++)
    		{
    		   number_string += '0';
    		}
    		return number_string;
    }
    Originally, I had...
    Code:
    var decimals_existing = number_string.length - number_string.indexOf('.') + 1;
    ...instead of...
    Code:
    var decimals_existing = number_string.length - decimal_position;
    ...but it didn't work. They should be equivalent, right?

    Anyway...already spent too much time on this!

    Thanks again.


Page 1 of 2 1 2 LastLast

Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 10:10 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2020 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com