javascript line

 4 Replies
 0 Subscribed to this topic
 52 Subscribed to this forum
Sort:
Author
Messages
TommyT
Veteran Member Send Private Message
Posts: 58
Veteran Member
Im having trouble with a piece of java_script....it needs  one more line
 
 dcell= "+" + addLeadingZeros(dcell,12);
if (DED_DED_AMT< 0 ){
 dcell = DED_DED_AMT;
 dcell= "-" + addLeadingZeros(dcell,12);
}
 
 
I need to display numbers in a format of +000000001.23.
 
Certain numbers will be negative and need to be displayed as -00000001.23
 
The code does everything right except when a number is negative it
is displaying -0000000-1.23
 
Can anyone help me with the line or 2  of code I need?
Robert Spurr
Veteran Member Send Private Message
Posts: 130
Veteran Member
I think what you are doing is correct except that once you have determined the amount to be negative you need to convert it to a postive number before adding the zeros.

dcell = DED_DED_AMT * -1;

Hope this helps.
George Graham
Veteran Member Send Private Message
Posts: 201
Veteran Member
You could also use the absolute function - abs().
TommyT
Veteran Member Send Private Message
Posts: 58
Veteran Member
I tried that and got -10.00 (no padding) on my line.
Robert Spurr
Veteran Member Send Private Message
Posts: 130
Veteran Member
I did a quick test in Design Studio to be sure and the example below returned the results you desire. (-000000010.45)
Hope this Helps.

var dblNegative = -10.45;
var dblPositive = dblNegative * -1;
dblPostive = "-" + addLeadingZeros(dblPositive,12);


function addLeadingZeros(number,length)
{
var str = '' + number;
while (str.length < length)
{
str = '0' + str;
}

return str;

}