By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,262 Members | 2,664 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,262 IT Pros & Developers. It's quick & easy.

leading zero

P: n/a
In my limited testing with FF 2, IE 6 and Opera 9 when I divided a
positive integer, that is less than 100, by 100 I get a leading zero in
front of the decimal point.

For example 80/100 gives 0.8

This is what the server app is expecting a leading zero (it does indeed
fail without it). Do I need to do anything to ensure the leading zero is
there?

Andrew Poulos
May 7 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Andrew Poulos wrote on 07 mei 2007 in comp.lang.javascript:
In my limited testing with FF 2, IE 6 and Opera 9 when I divided a
positive integer, that is less than 100, by 100 I get a leading zero in
front of the decimal point.
No you don't.
For example 80/100 gives 0.8
No it gives a number.
Only when you make a string of it, by writing it out,
you get the above, perhaps even depending on the regional settings.
This is what the server app is expecting a leading zero (it does indeed
fail without it).
The server application could expect something, but how the server gets the
result of your clientside computation, is the question?
Do I need to do anything to ensure the leading zero is there?
That depends on the above answer.

Would it be difficult for you to test that,
either or even both serverside or/and clientside?
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
May 7 '07 #2

P: n/a
Lee
Andrew Poulos said:
>
In my limited testing with FF 2, IE 6 and Opera 9 when I divided a
positive integer, that is less than 100, by 100 I get a leading zero in
front of the decimal point.

For example 80/100 gives 0.8

This is what the server app is expecting a leading zero (it does indeed
fail without it). Do I need to do anything to ensure the leading zero is
there?
Ideally, you should fix the server application so that it understands
how to parse numbers. If that's not feasible, complain to somebody
until they fix the server application so that it can parse numbers.
If that's not feasible, either, determine whether the lousy server
side software minds if there is more than one leading zero (eg "00.8"),
and if it doesn't, simply feed the lousy software an extra zero every
time ( value="0"+value ). Otherwise, you'll have to test the string
representation of the number to see if the first character is a ".",
and if it is, prepend a "0".
--

May 7 '07 #3

P: n/a
Evertjan. wrote:
Andrew Poulos wrote on 07 mei 2007 in comp.lang.javascript:
>In my limited testing with FF 2, IE 6 and Opera 9 when I divided a
positive integer, that is less than 100, by 100 I get a leading zero in
front of the decimal point.

No you don't.
>For example 80/100 gives 0.8

No it gives a number.
Only when you make a string of it, by writing it out,
you get the above, perhaps even depending on the regional settings.
>This is what the server app is expecting a leading zero (it does indeed
fail without it).

The server application could expect something, but how the server gets the
result of your clientside computation, is the question?
>Do I need to do anything to ensure the leading zero is there?

That depends on the above answer.

Would it be difficult for you to test that,
either or even both serverside or/and clientside?
I can't test serverside (except in a limited case locally) because its
for a client who hasn't yet decided on their SCORM compliant LMS.

The SCORM 2004 spec wants the data type I'm needing to send to be
calculated as a number between 1.0 and -1.0 and then converted to a
string before it is sent. The format required is either a leading 1
(followed by a decimal point and then a 0 i.e. "1.0") or a 0 followed by
a decimal point and then 1 or more digits eg. "0.875". [Though it
doesn't state if 0 should be 0.0 or just 0.]

Andrew Poulos
May 7 '07 #4

P: n/a
Andrew Poulos said the following on 5/7/2007 6:18 PM:
Evertjan. wrote:
>Andrew Poulos wrote on 07 mei 2007 in comp.lang.javascript:
>>In my limited testing with FF 2, IE 6 and Opera 9 when I divided a
positive integer, that is less than 100, by 100 I get a leading zero
in front of the decimal point.

No you don't.
>>For example 80/100 gives 0.8

No it gives a number.
Only when you make a string of it, by writing it out,
you get the above, perhaps even depending on the regional settings.
>>This is what the server app is expecting a leading zero (it does
indeed fail without it).

The server application could expect something, but how the server gets
the result of your clientside computation, is the question?
>>Do I need to do anything to ensure the leading zero is there?

That depends on the above answer.

Would it be difficult for you to test that, either or even both
serverside or/and clientside?

I can't test serverside (except in a limited case locally) because its
for a client who hasn't yet decided on their SCORM compliant LMS.
Test it client side before sending then.
The SCORM 2004 spec wants the data type I'm needing to send to be
calculated as a number between 1.0 and -1.0 and then converted to a
string before it is sent. The format required is either a leading 1
(followed by a decimal point and then a 0 i.e. "1.0") or a 0 followed by
a decimal point and then 1 or more digits eg. "0.875". [Though it
doesn't state if 0 should be 0.0 or just 0.]
Convert it to a String, check indexOf('.'). If it is 0, then add the
leading 0, if it is 1 then all is fine. If it is 2 then....

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
May 7 '07 #5

P: n/a
In comp.lang.javascript message <Xn********************@194.109.133.242>
, Mon, 7 May 2007 13:51:55, Evertjan. <ex**************@interxnl.net>
posted:
>
>For example 80/100 gives 0.8

No it gives a number.
Only when you make a string of it, by writing it out,
you get the above, perhaps even depending on the regional settings.
ECMA-262 and ISO 16262 sections 9.8.1 7 permit no variation.

Much code would break if the result of Number.toString did not match a
number literal in code.
General : IUPAP, SUNAMCO, &/or suchlike strongly recommend that, in
test, a decimal separator should always have a digit on each side. The
same should be applied, where practical, in data.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/- FAQqish topics, acronyms & links;
Astro stuff via astron-1.htm, gravity0.htm ; quotings.htm, pascal.htm, etc.
No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.
May 7 '07 #6

P: n/a
Andrew Poulos wrote on 08 mei 2007 in comp.lang.javascript:
I can't test serverside (except in a limited case locally) because its
for a client who hasn't yet decided on their SCORM compliant LMS.

The SCORM 2004 spec wants the data type I'm needing to send to be
calculated as a number between 1.0 and -1.0 and then converted to a
string before it is sent. The format required is either a leading 1
(followed by a decimal point and then a 0 i.e. "1.0") or a 0 followed by
a decimal point and then 1 or more digits eg. "0.875". [Though it
doesn't state if 0 should be 0.0 or just 0.]
I doubt if any form of js ever would skip that zero,
but this would correct it:

n = '-.3'

r = (Math.abs(n)+10)+''
r = r.replace(/\d(\d\.)/,'$1')
r = ((n<0)?'-':'') + r

alert(r)
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
May 7 '07 #7

P: n/a
In comp.lang.javascript message <Vu********************@giganews.com>,
Mon, 7 May 2007 18:29:59, Randy Webb <Hi************@aol.composted:
>The SCORM 2004 spec wants the data type I'm needing to send to be
calculated as a number between 1.0 and -1.0 and then converted to a
string before it is sent. The format required is either a leading 1
(followed by a decimal point and then a 0 i.e. "1.0") or a 0 followed
by a decimal point and then 1 or more digits eg. "0.875". [Though it
doesn't state if 0 should be 0.0 or just 0.]
If you've quoted it adequately, it does so state; the first character
(disregarding sign) must be 0 or 1, then there must be a point, then at
least one digit.
>Convert it to a String, check indexOf('.'). If it is 0, then add the
leading 0, if it is 1 then all is fine. If it is 2 then....
Eh?

The following will take any possible Number (AFAICS), including NaN and
Infinities, and output it in a sensible form with a decimal point where
possible. It assumes Number.toString is ECMA-compliant or better.

Str = String(Num).replace(/^(-?\d+)$/, "$1.0")

If the OP's code, at whatever stage of development, should happen to
generate a Number of magnitude greater than 1.0, it's probably desirable
that the transformation should not add confusion by doing further
damage.

StrS(Num, 1, 1), via the FAQ, would do it, with a simpler Sign function;
but that's overkill unless needed otherwise. But the application may be
such that 1/7 should be presented as, say, "0.143" rather than
"0.14285714285714285".

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/- FAQqish topics, acronyms & links;
Astro stuff via astron-1.htm, gravity0.htm ; quotings.htm, pascal.htm, etc.
No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.
May 8 '07 #8

P: n/a
In comp.lang.javascript message <Xn*******************@194.109.133.242>,
Mon, 7 May 2007 22:44:16, Evertjan. <ex**************@interxnl.net>
posted:
>Andrew Poulos wrote on 08 mei 2007 in comp.lang.javascript:
>The SCORM 2004 spec wants the data type I'm needing to send to be
calculated as a number between 1.0 and -1.0 and then converted to a
string before it is sent. The format required is either a leading 1
(followed by a decimal point and then a 0 i.e. "1.0") or a 0 followed by
a decimal point and then 1 or more digits eg. "0.875". [Though it
doesn't state if 0 should be 0.0 or just 0.]

I doubt if any form of js ever would skip that zero,
but this would correct it:

n = '-.3'

r = (Math.abs(n)+10)+''
r = r.replace(/\d(\d\.)/,'$1')
r = ((n<0)?'-':'') + r

alert(r)
Granted that it is unclear whether the OP's final . is decimal or period
- I think this ensures a pre-decimal digit :-

r = n.replace(/(^|\D)\./, "$10.")

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/- FAQqish topics, acronyms & links;
Astro stuff via astron-1.htm, gravity0.htm ; quotings.htm, pascal.htm, etc.
No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.
May 8 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.