leading zero

A

Andrew Poulos

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
 
E

Evertjan.

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?
 
L

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".


--
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]>
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.
 
A

Andrew Poulos

Evertjan. said:
Andrew Poulos wrote on 07 mei 2007 in comp.lang.javascript:


No you don't.


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.


The server application could expect something, but how the server gets the
result of your clientside computation, is the question?


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
 
E

Evertjan.

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)
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]>,
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".
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]>,
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.")
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,431
Latest member
ElyseG3173

Latest Threads

Top