+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 15

Thread: Padding with zeros

  1. #1
    Barn Enthusiast guddu is on a distinguished road guddu's Avatar
    Join Date
    Jul 2008
    Location
    Oxford UK
    Posts
    471
    Rep Power
    4

    Padding with zeros

    hi
    below is javascript function used to padd no of zeros.this is working for positive nos but in case of negative nos not working.

    let me explain
    suppose no is like this 47.51
    and i m calling function like this javascript:msPadNumber(47.51, 12, 2)
    i m getting output like this 000000004751 which is correct.

    but suppose i call negative no like this javascript:msPadNumber(-47.51, 12, 2)
    i m getting output like this 0000000-4751. but i want output like this
    -00000004751.

    hope it make some sense.
    how to get this?

    Code:
    function msPadNumber(vvNumber, vlLength, vlDPs)
    {
    	var sNumber = '';
    	
    	try
    	{
    		sNumber = vvNumber(0).text;
    	}
    	catch(e)
    	{
    		sNumber = vvNumber.toString();
    	}
    	
    	if(sNumber.indexOf('.') != -1)
    	{
    		var lDPs = sNumber.length - sNumber.indexOf('.') - 1;
    		
    		if(lDPs > vlDPs)
    		{
    			sNumber = sNumber.substr(0, sNumber.length + vlDPs - lDPs);
    			vlDPs = 0;
    		}
    		else
    		{
    			vlDPs -= lDPs;
    		} 
    	}
    	
    	for(var i=0; i<vlDPs; i++)
    	{
    		sNumber += '0';
    	}
    	
    	sNumber = sNumber.replace('.','');
    	
    	while(sNumber.length < vlLength)
    	{
    		sNumber = '0' + sNumber;
    	}
    	
    	return sNumber.substr(0, vlLength);
    		
    }
    
    Love is physical attraction and mental destruction

  2. #2
    Super Sarcasm Mistress mehere is a glorious beacon of light mehere is a glorious beacon of light mehere is a glorious beacon of light mehere is a glorious beacon of light mehere is a glorious beacon of light mehere's Avatar
    Join Date
    Mar 2008
    Location
    Wide Awake In Dreamland
    Posts
    830
    Rep Power
    8

    use an if statement to see if the number is less than zero, then remove the negative sign, pad your number and add the negative sign back in.
    Quote of the Month:
    INSIGHT: When the going gets tough, the tough get going. The smart left a long time ago.

    Questions to Ponder:
    Are people more violently opposed to fur rather than leather because it's much easier to harass rich women than motorcycle gangs?

    iif([sarcasm]=true,iif([you have to ask]=true,"didn't work","ha ha ha"),"not sarcasm")
    copyright © 2008 sbenj69

    Sarchasm: The gulf between the author of sarcastic wit and the person who doesn't get it.

  3. #3
    Barn Enthusiast guddu is on a distinguished road guddu's Avatar
    Join Date
    Jul 2008
    Location
    Oxford UK
    Posts
    471
    Rep Power
    4

    how to remove and add sign back in javascript?
    Love is physical attraction and mental destruction

  4. #4
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    Something like this:-
    Code:
    var neg = false;
    if(sNumber.search(/-/)!=-1){
    sNumber.replace(/-/,'');
    neg=true;
    }
     
     
    if(neg) sNumber = '-';
    

  5. #5
    Barn Enthusiast guddu is on a distinguished road guddu's Avatar
    Join Date
    Jul 2008
    Location
    Oxford UK
    Posts
    471
    Rep Power
    4

    hi @RR

    for time being i used only this part of code in mine
    Code:
    try
    			{
    				sNumber = vvNumber(0).text;
    			}
    			catch(e)
    			{
    				sNumber = vvNumber.toString();
    			}
    			
    			var neg = false;
    			if(sNumber.search(/-/)!=-1){
    			sNumber.replace(/-/,'');
    			neg=true;
    			}
    -------Rest of code
    
    so that the final output will not contain - sign.but still i m getting - sign in my output
    Love is physical attraction and mental destruction

  6. #6
    Barn Enthusiast guddu is on a distinguished road guddu's Avatar
    Join Date
    Jul 2008
    Location
    Oxford UK
    Posts
    471
    Rep Power
    4

    ok
    this should be like this
    Code:
    var neg = false;
    if(sNumber.search(/-/)!=-1){
    sNumber = sNumber.replace(/-/,'');
    neg=true;
    }
    
    Love is physical attraction and mental destruction

  7. #7
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    Quote Originally Posted by guddu View Post
    ok
    this should be like this
    Code:
    var neg = false;
    if(sNumber.search(/-/)!=-1){
    sNumber = sNumber.replace(/-/,'');
    neg=true;
    }
    
    Ah, yes...Sorry about that...Good spot

  8. #8
    Barn Enthusiast guddu is on a distinguished road guddu's Avatar
    Join Date
    Jul 2008
    Location
    Oxford UK
    Posts
    471
    Rep Power
    4

    hi
    Guys there is issue with this code on live.
    for this example
    suppose I call negative no like this javascript:msPadNumber(-47.51, 12, 2)
    I want output like this
    -00000004751.

    but currently i m getting output like this
    -00000000475

    the negative sign is incorrectly causing the last digit to be dropped.so 1 is missing in my output
    Love is physical attraction and mental destruction

  9. #9
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    It's a bit early in the morning for me to work this through, but I think it's to do with this line:-
    Code:
    var lDPs = sNumber.length - sNumber.indexOf('.') - 1;
    
    indexOf starts at 0 so I think the positioning will be 1 to the left of the position you think it's at and combined with the length which would start at 1, it's pushing it one place to the left.

  10. #10
    Barn Enthusiast guddu is on a distinguished road guddu's Avatar
    Join Date
    Jul 2008
    Location
    Oxford UK
    Posts
    471
    Rep Power
    4

    what should I do now?
    Love is physical attraction and mental destruction

+ Reply to Thread
Page 1 of 2 1 2 LastLast

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

SEO by vBSEO