Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to remove trailing decimal or decimal 0 of a string?

83 views
Skip to first unread message

skrite

unread,
Nov 16, 2012, 12:12:49 PM11/16/12
to
Javascript: Hey all, if myString = "11." i would like it to be just "11"
if it is "11.0" i would also like it to just be "11"
however, if the string ends with anything else, like "11.3" or "11.34" i would like to leave it the way it is. Basically, i want to remove the decimal point and trailing 0 of any of my strings that are whole numbers.

Andrew Poulos

unread,
Nov 16, 2012, 3:10:42 PM11/16/12
to
How about:

var s= "11.";
var i = parseInt(s, 10);
var f = parseFloat(s);

s = (i == f ? i : f) + "";

Andrew Poulos

Andrew Poulos

unread,
Nov 16, 2012, 9:51:54 PM11/16/12
to
On 17/11/2012 7:10 AM, Andrew Poulos wrote:> On 17/11/2012 4:12 AM,
Or maybe

s = (f % 1 ? f : s) + "";

Andrew Poulos

Denis McMahon

unread,
Nov 17, 2012, 3:05:17 AM11/17/12
to
I did the following tests in the javascript console:

--
if (Math.floor(parseFloat("10.5")) == parseFloat("10.5")) Math.floor
(parseFloat("10.5")).toString(); else parseFloat("10.5").toString();
"10.5"
--
if (Math.floor(parseFloat("10.")) == parseFloat("10.")) Math.floor
(parseFloat("10.")).toString(); else parseFloat("10.").toString();
"10"
--
if (Math.floor(parseFloat("10")) == parseFloat("10")) Math.floor
(parseFloat("10")).toString(); else parseFloat("10").toString();
"10"

So assuming you're starting with a numeric value written as a string, and
you want to end up with a numeric value written as a string, something
like the following might work:

x = parseFloat( myString );

if ( Math.floor( x ) == x )
myString = Math.floor( x ).toString();
else
myString = x.toString();

Rgds

Denis McMahon

Evertjan.

unread,
Nov 17, 2012, 3:24:39 AM11/17/12
to
n = n.replace(/\.0*/,'');

======================

This will also replace trailing 0's in 11.02000

n = n.replace(/(\.\d*[1-9])0+/,'$1').replace(/\.0*/,'');




--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Evertjan.

unread,
Nov 17, 2012, 3:29:37 AM11/17/12
to
Evertjan. wrote on 17 nov 2012 in comp.lang.javascript:

> skrite wrote on 16 nov 2012 in comp.lang.javascript:
>
>> [..]Basically, i want to remove
>> the decimal point and trailing 0 of any of my strings that are whole
>> numbers.
>
> n = n.replace(/\.0*/,'');

Sorry, should be:

n = n.replace(/\.0*$/,'');


> ======================
>
> This will also replace trailing 0's in 11.02000
>
> n = n.replace(/(\.\d*[1-9])0+/,'$1').replace(/\.0*/,'');

and:

n = n.replace(/(\.\d*[1-9])0+$/,'$1').replace(/\.0*$/,'');

Christoph Becker

unread,
Nov 17, 2012, 6:04:55 AM11/17/12
to
Can't one not simply do the following?

myNumber = +myString; // convert string to numeric value
myString = "" + myNumber; // convert numeric value back to string, if at
all necessary

--
Christoph M. Becker

Thomas 'PointedEars' Lahn

unread,
Nov 17, 2012, 9:37:02 AM11/17/12
to
Andrew Poulos wrote:

> On 17/11/2012 4:12 AM, skrite wrote:
>> Javascript: Hey all, if myString = "11." i would like it to be just
>> "11" if it is "11.0" i would also like it to just be "11" however, if
>> the string ends with anything else, like "11.3" or "11.34" i would
>> like to leave it the way it is. Basically, i want to remove the
>> decimal point and trailing 0 of any of my strings that are whole
>> numbers.
>
> How about:
>
> var s= "11.";
> var i = parseInt(s, 10);

This will convert "11." to a Number value, stopping at `.'. However, the OP
wanted decimals to be preserved.

> var f = parseFloat(s);

This will convert "11." to a Number value; preserving decimals, if any.

> s = (i == f ? i : f) + "";

So there is no point in this with regard to the question.


PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f806at$ail$1$8300...@news.demon.co.uk>

BootNic

unread,
Nov 17, 2012, 11:28:26 AM11/17/12
to
In article <e89247f7-aa7b-4c64...@googlegroups.com>,
Perhaps convert the string to number:

myString = Number(myString);

if it needs to be a string:

myString = String(Number(myString));


--
BootNic Sat Nov 17, 2012 11:28 am
Our earth is degenerate in these latter days; bribery and corruption are
common; children no longer obey their parents; and the end of the world is
evidently approaching.
*Assyrian clay tablet 2800 B.C.*
signature.asc

Andrew Poulos

unread,
Nov 17, 2012, 4:28:11 PM11/17/12
to
On 18/11/2012 1:37 AM, Thomas 'PointedEars' Lahn wrote:
> Andrew Poulos wrote:
>
>> On 17/11/2012 4:12 AM, skrite wrote:
>>> Javascript: Hey all, if myString = "11." i would like it to be
>>> just "11" if it is "11.0" i would also like it to just be "11"
>>> however, if the string ends with anything else, like "11.3" or
>>> "11.34" i would like to leave it the way it is. Basically, i want
>>> to remove the decimal point and trailing 0 of any of my strings
>>> that are whole numbers.
>>
>> How about:
>>
>> var s= "11."; var i = parseInt(s, 10);
>
> This will convert "11." to a Number value, stopping at `.'. However,
> the OP wanted decimals to be preserved.
>
>> var f = parseFloat(s);
>
> This will convert "11." to a Number value; preserving decimals, if
> any.
>
>> s = (i == f ? i : f) + "";
>
> So there is no point in this with regard to the question.

In my testing with

window.alert(parseFloat("11."));
window.alert(parseFloat("11.0"));
window.alert(parseFloat("11.00"));
window.alert(parseFloat("11.000"));
window.alert(parseFloat("11.0000"));

"11" was always returned ie. if the decimal part of the number is "zero"
then its not preserved.

Andrew Poulos

Dr J R Stockton

unread,
Nov 17, 2012, 4:08:49 PM11/17/12
to
In comp.lang.javascript message <e89247f7-aa7b-4c64-9de5-265ab78a5336@go
oglegroups.com>, Fri, 16 Nov 2012 09:12:49, skrite <sh...@skrite.net>
posted:
S = "11." // etc.
S = S.replace(/\.0*$/, "") // should work in all JS systems.

But remember that JavaScript numbers are floats, so do that after
rounding to the desired resolution.

IMHO, it is better practice to represent all numbers of a "set" with the
same number of decimal places, and never to have a decimal point which
is not surrounded by digits.

See the book described in <http://www.iupap.org/commissions/c2/redbook/>
- in my printed 1987 copy, it is section 1.3.2.

<http://fias.uni-frankfurt.de/~hees/tmp/iupap-symbols-typography.pdf>
appears to be a copy of a similar but slightly different document; but
its 1.3.2 matches.

--
(c) John Stockton, near London. Mail ?.?.Stoc...@physics.org
Web <http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, and links.

Thomas 'PointedEars' Lahn

unread,
Nov 17, 2012, 7:02:06 PM11/17/12
to
Andrew Poulos wrote:

> On 18/11/2012 1:37 AM, Thomas 'PointedEars' Lahn wrote:
>> Andrew Poulos wrote:
>>> On 17/11/2012 4:12 AM, skrite wrote:
>>>> Javascript: Hey all, if myString = "11." i would like it to be
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>>>> just "11" if it is "11.0" i would also like it to just be "11"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>>>> however, if the string ends with anything else, like "11.3" or
>>>> "11.34" i would like to leave it the way it is. Basically, i want
^^^^^^^^^^^^^^^^^
>>>> to remove the decimal point and trailing 0 of any of my strings
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>>>> that are whole numbers.
^^^^^^^^^^^^^^^^^^^^^^^
>>>
>>> How about:
>>>
>>> var s= "11."; var i = parseInt(s, 10);
>>
>> This will convert "11." to a Number value, stopping at `.'. However,
>> the OP wanted decimals to be preserved.
>>
>>> var f = parseFloat(s);
>>
>> This will convert "11." to a Number value; preserving decimals, if
>> any.
>>
>>> s = (i == f ? i : f) + "";
>>
>> So there is no point in this with regard to the question.
> In my testing with
>
> window.alert(parseFloat("11."));
> window.alert(parseFloat("11.0"));
> window.alert(parseFloat("11.00"));
> window.alert(parseFloat("11.000"));
> window.alert(parseFloat("11.0000"));
>
> "11" was always returned ie. if the decimal part of the number is "zero"
> then its not preserved.

Which is exactly what the OP was asking for, so

s = parseFloat(s) + "";

is sufficient [although I would prefer String(parseFloat(s)) or skip the
explicit conversion altogether], and you can skip the variable declarations
and initializations for `i' and `f'.


PointedEars
--
Sometimes, what you learn is wrong. If those wrong ideas are close to the
root of the knowledge tree you build on a particular subject, pruning the
bad branches can sometimes cause the whole tree to collapse.
-- Mike Duffy in cljs, <news:Xns9FB6521286...@94.75.214.39>

Thomas 'PointedEars' Lahn

unread,
Nov 17, 2012, 7:56:31 PM11/17/12
to
BootNic wrote:

> skrite <sh...@skrite.net> wrote:
>> Javascript: Hey all, if myString = "11." i would like it to be just
>> "11" if it is "11.0" i would also like it to just be "11" however, if
>> the string ends with anything else, like "11.3" or "11.34" i would like
>> to leave it the way it is. Basically, i want to remove the decimal
>> point and trailing 0 of any of my strings that are whole numbers.
>
> Perhaps convert the string to number:

You are a bit late to the party, but your posting gives me reason to expand
on this:

> myString = Number(myString);

Contrary to popular belief, the different approaches to string-to-number
conversion are _not_ equivalent:

Number(myString) will return NaN if there are any non-digits and non-dots in
myString, ignoring leading whitespace. It will parse (the value of)
`myString' as a hexadecimal number if it starts (after leading whitespace)
with `0x', and will return NaN if there are any non-hexadecimal digits in
the string (including the decimal point). It will _not_ convert `myString'
to String if it is not one.

+myString will do the same as Number(myString). The "Unary +" operator was
introduced in the first Edition (1997) as was `Number', but it was not
implemented before JavaScript 1.3 (1998) and JScript 3.1.3510 (1999).
(CAVEAT: Per older, non-scientific research. My current, overall more
complete research [not yet online] did not include those implementations
because of backwards compatibility issues; but I intend to fix that.)

parseInt(myString) will first convert `myString' to String(!) before parsing
it as a number of unspecified base. The base is determined by the first
leading non-whitespace sequence: if it is `0x', the base is 16
(hexadecimal), in older implementations (of Edition 3 and before) if it is
`0' the base would be 8 (octal; in Ed. 2 mandatory, in Ed. 3 implementation-
dependent), otherwise 10 (decimal). It will stop parsing before the first
character that it cannot interpret according to the determined base. For
those reasons, parseInt() should not be called like this.

parseInt(myString, base) will first convert `myString' to String before
parsing it as a number of the specified base, while ignoring `0x' (after
leading whitespace) if base == 16. It will only return NaN if no digits
could be found before the first non-digit (ignoring leading whitespace).

parseFloat(myString) will first convert `myString' to String before parsing
it as a decimal number, and it will return NaN if that is not possible.
Leading whitespace will be ignored. (The built-in parseFloat() cannot parse
non-decimal fractions; see jsx.math.parseFloat() for that.)

Therefore, parseFloat(myString) is probably the best solution here.

The forced conversion of the argument value to String is also important
because the string representation of Number values is implementation-
dependent. Thus,

parseFloat(n) === n

is not necessarily true for all Number values `n'.

> if it needs to be a string:
>
> myString = String(Number(myString));

Same problem. For defined (and potentially more exact) string
representations of Number values, ECMAScript Ed. 3 to 5.1 specify
Number.prototype.toFixed(), Number.prototype.toExponential(), and
Number.prototype.toPrecision().


PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee

Andrew Poulos

unread,
Nov 17, 2012, 11:40:17 PM11/17/12
to
Dang, I thought I had one over on you :-)

Andrew Poulos

Dr J R Stockton

unread,
Nov 19, 2012, 2:53:25 PM11/19/12
to
In comp.lang.javascript message <1788183.L...@PointedEars.de>,
Sun, 18 Nov 2012 01:56:31, Thomas 'PointedEars' Lahn
<Point...@web.de> posted:

>
>Number(myString) will return NaN if there are any non-digits and non-dots in
>myString, ignoring leading whitespace.

Trebly untrue, even disregarding the omissions of "decimal" and of
trailing whitespace.

And, of course, those of the digits i v I V etc. which are in UniCode
0000-FFFF, and IIRC various even stranger digits, including \u261D.

--
(c) John Stockton, nr London, UK. For Mail, see Home Page. Turnpike, WinXP.
Web <http://www.merlyn.demon.co.uk/> - FAQ-type topics, acronyms, and links.
Command-prompt MiniTrue is useful for viewing/searching/altering files. Free,
DOS/Win/UNIX now 2.0.6; see <URL:http://www.merlyn.demon.co.uk/pc-links.htm>.

Andrew Poulos

unread,
Nov 19, 2012, 7:03:55 PM11/19/12
to
On 20/11/2012 6:53 AM, Dr J R Stockton wrote:
> In comp.lang.javascript message <1788183.L...@PointedEars.de>,
> Sun, 18 Nov 2012 01:56:31, Thomas 'PointedEars' Lahn
> <Point...@web.de> posted:
>
>> Number(myString) will return NaN if there are any non-digits and non-dots in
>> myString, ignoring leading whitespace.
>
> Trebly untrue, even disregarding the omissions of "decimal" and of
> trailing whitespace.

On IE 9 at least, PE seems to be correct:

var myString = "33a";
window.alert(Number(myString)); // alerts NaN

myString = "3 3";
window.alert(Number(myString)); // alerts NaN

myString = "33-";
window.alert(Number(myString)); // alerts NaN

myString = " 33";
window.alert(Number(myString)); // alerts 3

myString = "33 ";
window.alert(Number(myString)); // alerts 3

> And, of course, those of the digits i v I V etc. which are in UniCode
> 0000-FFFF, and IIRC various even stranger digits, including \u261D.

Aren't those referred to as "Roman numerals"? "Digits" are any of the
Arabic figures of 1 through 9 and 0.

Andrew Poulos

Thomas 'PointedEars' Lahn

unread,
Nov 19, 2012, 7:04:40 PM11/19/12
to
Dr J R Stockton wrote:

> Thomas 'PointedEars' Lahn <Point...@web.de> posted:
>> Number(myString) will return NaN if there are any non-digits and non-dots
>> in myString, ignoring leading whitespace.
>
> Trebly untrue, even disregarding the omissions of "decimal" and of
> trailing whitespace.

Pray tell. Does Number(",") return NaN? Does Number(" ,") return NaN?
Does Number(" 42") return 42?

You have conveniently snipped the part of my posting that qualified this
statement (without marking the omission). You really are to be pitied if
you think have to resort to such lame tricks to get attention.

> And, of course, those of the digits i v I V etc. which are in UniCode
> 0000-FFFF, and IIRC various even stranger digits, including \u261D.

Don't be silly.


PointedEars
--
When all you know is jQuery, every problem looks $(olvable).

Thomas 'PointedEars' Lahn

unread,
Nov 19, 2012, 7:50:16 PM11/19/12
to
Andrew Poulos wrote:

> myString = " 33";
> window.alert(Number(myString)); // alerts 3
>
> myString = "33 ";
> window.alert(Number(myString)); // alerts 3

I do hope that it alerts 33, otherwise my to-do list for the ES Matrix would
get even longer ;-)

>> And, of course, those of the digits i v I V etc. which are in UniCode
>> 0000-FFFF, and IIRC various even stranger digits, including \u261D.
>
> Aren't those referred to as "Roman numerals"? "Digits" are any of the
> Arabic figures of 1 through 9 and 0.

In theory, a digit is any symbol in a positional numeral system (from the
ancient Latin word for "finger"; BTW, as I have learned recently while
studying IEEE 754 again, the word for using such a system is *algorism*(!)),
which Roman numerals are not (it is an additive system instead). That
includes, but is not limited to, decimal digits. (There are hexadecimal
digits A to F, for example.)

However, without further qualification "digit" is commonly understood,
indeed, to be limited to those ten symbols which in Europe evolved from
Arabic writing (through Al-Khwārizmī, ca. 780-850 CE), but originated in
India (the digit and number zero in particular, probably starting from
Aryabhata ca. 476–550 CE going back to Jain mathematics ca. 400 BCE; BTW,
there was a fascinating BBC documentation about this years ago – I have
forgotten the title).

The latter is what I meant here. (The following sentences of my posting did
expand on possible exceptions for hexadecimal digits with Number(…).)


HTH

PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm> (404-comp.)

Christoph Becker

unread,
Nov 19, 2012, 8:10:31 PM11/19/12
to
Thomas 'PointedEars' Lahn wrote:
> Contrary to popular belief, the different approaches to string-to-number
> conversion are _not_ equivalent:
> [...]
> Therefore, parseFloat(myString) is probably the best solution here.
> [...]

Thank you very much for your most informative answer. :) It clearly
showed, that my uncertainty about using +myString was appropriate, and
that better solutions to the problem at hand do exist (obviously no
"perfect" one, though).

FWIW: I've noticed a /minor/ (as it's not related to the OP's question)
omission in your explanations: all(?) of the conversion routines will
recognize a leading sign (+/-) and will ignore trailing whitespace.

--
Christoph M. Becker

Thomas 'PointedEars' Lahn

unread,
Nov 20, 2012, 3:58:58 PM11/20/12
to
Christoph Becker wrote:

> Thomas 'PointedEars' Lahn wrote:
>> Contrary to popular belief, the different approaches to string-to-number
>> conversion are _not_ equivalent:
>> [...]
>> Therefore, parseFloat(myString) is probably the best solution here.
>> [...]
>
> Thank you very much for your most informative answer. :) It clearly
> showed, that my uncertainty about using +myString was appropriate, and
> that better solutions to the problem at hand do exist (obviously no
> "perfect" one, though).

You are welcome.

> FWIW: I've noticed a /minor/ (as it's not related to the OP's question)
> omission in your explanations: all(?) of the conversion routines will
> recognize a leading sign (+/-) and will ignore trailing whitespace.

That is a very good question.

Actually, Number(" -0x20 "), Number(" +0x20 "), +" -0x20 ", and +" +0x20 "
all result in NaN, while Number(" 0x20 ") and +" 0x20 " both result in 32,
parseInt(" -0x20 ", 16) returns -32, and parseInt(" +0x20 ", 16) returns 32
(with and without specified base) in Chromium 22.0.1229.94 on GNU/Linux.

That complies with ECMAScript Ed. 5.1, sections 9.3.1 and 15.1.2.2, and the
following relevant productions in the former:

| StringNumericLiteral :::
| StrWhiteSpace_opt
| StrWhiteSpace_opt StrNumericLiteral StrWhiteSpace_opt
|
| […]
|
| StrNumericLiteral :::
| StrDecimalLiteral
| HexIntegerLiteral
|
| […]
|
| HexIntegerLiteral :::
| 0x HexDigit
| 0X HexDigit
| HexIntegerLiteral HexDigit
|
| HexDigit ::: one of
| 0 1 2 3 4 5 6 7 8 9 a b c d e f A B C D E F

As for the trailing whitespace, though, for parseInt() and parseFloat() that
is already covered by the explanation that parsing stops before the first
character that does not belong to the determined or specified numeric
literal [§15.1.2.2, 11.; §15.1.2.3, 4.].

Dr J R Stockton

unread,
Nov 21, 2012, 1:44:14 PM11/21/12
to
In comp.lang.javascript message <1-ydnQHMtu9oVTfNnZ2dnUVZ_hednZ2d@westne
t.com.au>, Tue, 20 Nov 2012 11:03:55, Andrew Poulos
<ap_...@hotmail.com> posted:

>On 20/11/2012 6:53 AM, Dr J R Stockton wrote:
>> In comp.lang.javascript message <1788183.L...@PointedEars.de>,
>> Sun, 18 Nov 2012 01:56:31, Thomas 'PointedEars' Lahn
>> <Point...@web.de> posted:
>>
>>> Number(myString) will return NaN if there are any non-digits and non-dots in
>>> myString, ignoring leading whitespace.
>>
>> Trebly untrue, even disregarding the omissions of "decimal" and of
>> trailing whitespace.
>
>On IE 9 at least, PE seems to be correct:
>
>var myString = "33a";
>window.alert(Number(myString)); // alerts NaN
>
>myString = "3 3";
>window.alert(Number(myString)); // alerts NaN
>
>myString = "33-";
>window.alert(Number(myString)); // alerts NaN
>
>myString = " 33";
>window.alert(Number(myString)); // alerts 3
>
>myString = "33 ";
>window.alert(Number(myString)); // alerts 3


You are merely proving that some non-digit non-dot characters will
result in NaN, which is not the same thing. To prove the quoted
assertion, you need to consider all possible non-s in all possible
places. But to disprove an "any" statement, a single failure suffices.


>
>> And, of course, those of the digits i v I V etc. which are in UniCode
>> 0000-FFFF, and IIRC various even stranger digits, including \u261D.
>
>Aren't those referred to as "Roman numerals"? "Digits" are any of the
>Arabic figures of 1 through 9 and 0.


Read my article again, noticing the word "disregarding". However, those
characters are in some sense digits, and a precisionist like Lahn (is
anyone like him? I hope not) should have remembered them.

--
(c) John Stockton, nr London, UK. Mail via homepage. Turnpike v6.05 MIME.
Web <http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms and 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.

Dr J R Stockton

unread,
Nov 21, 2012, 2:00:40 PM11/21/12
to
In comp.lang.javascript message <1538569.g...@PointedEars.de>,
Tue, 20 Nov 2012 01:04:40, Thomas 'PointedEars' Lahn
<Point...@web.de> posted:

>Dr J R Stockton wrote:
>
>> Thomas 'PointedEars' Lahn <Point...@web.de> posted:
>>> Number(myString) will return NaN if there are any non-digits and non-dots
>>> in myString, ignoring leading whitespace.
>>
>> Trebly untrue, even disregarding the omissions of "decimal" and of
>> trailing whitespace.
>
>Pray tell. Does Number(",") return NaN? Does Number(" ,") return NaN?
>Does Number(" 42") return 42?
>
>You have conveniently snipped the part of my posting that qualified this
>statement (without marking the omission). You really are to be pitied if
>you think have to resort to such lame tricks to get attention.

You made a definite false statement, and no subsequent qualification
negates that. A skilled author would have put that statement last,
preceded by "Otherwise", or similar; or would have used "will generally
return".


But I must apologise; it was at least _quadruply_ untrue.


Who can list four or more distinct cases in which Number(myString) will
**NOT** return NaN although there are non-(digits or dots) in myString,
ignoring leading and trailing whitespace?



>> And, of course, those of the digits i v I V etc. which are in UniCode
>> 0000-FFFF, and IIRC various even stranger digits, including \u261D.

That was to amuse the more intelligent, and to inform those unaware of
Unicode i-xii I-XII characters. You should have ignored it.

--
(c) John Stockton, nr London UK. Mail, see homepage. DOS 3.3, 6.20; WinXP, 7.
Web <http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms & links.
PAS EXE TXT ZIP via <http://www.merlyn.demon.co.uk/programs/00index.htm>
My DOS <http://www.merlyn.demon.co.uk/batfiles.htm> - also batprogs.htm.

Dr J R Stockton

unread,
Nov 21, 2012, 2:09:22 PM11/21/12
to
In comp.lang.javascript message <k8el8d$l71$1...@speranza.aioe.org>, Tue,
20 Nov 2012 02:10:31, Christoph Becker <cmbec...@gmx.de> posted:

>Thomas 'PointedEars' Lahn wrote:
>> Contrary to popular belief, the different approaches to string-to-number
>> conversion are _not_ equivalent:
>> [...]
>> Therefore, parseFloat(myString) is probably the best solution here.
>> [...]
>
>Thank you very much for your most informative answer. :) It clearly
>showed, that my uncertainty about using +myString was appropriate, and
>that better solutions to the problem at hand do exist (obviously no
>"perfect" one, though).

But +myString is entirely safe, by definition, where myString looks like
a decimal number. One just needs to be sure that the preceding
character is not a +, AFAIR; I would always use whitespace or = there.

Using anything other conversion, except where necessary, is either mere
bloat or pandering to uninformed and unintelligent readers.

Newbies should note that is often unwise to use ANY of those methods on
a string directly typed in to a control without first checking the
format of the string with a Regular Expression; and that, after suitable
checking and in ordinary circumstances, any of the methods will work
well.

--
(c) John Stockton, nr London, UK. E-mail, see Home Page. Turnpike v6.05.
Website <http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc. : <http://www.merlyn.demon.co.uk/programs/> - see in 00index.htm
Dates - miscdate.htm estrdate.htm js-dates.htm pas-time.htm critdate.htm etc.

Andrew Poulos

unread,
Nov 21, 2012, 10:07:00 PM11/21/12
to
On 22/11/2012 5:44 AM, Dr J R Stockton wrote:
> In comp.lang.javascript message <1-ydnQHMtu9oVTfNnZ2dnUVZ_hednZ2d@westne
> t.com.au>, Tue, 20 Nov 2012 11:03:55, Andrew Poulos
> <ap_...@hotmail.com> posted:
>
>> On 20/11/2012 6:53 AM, Dr J R Stockton wrote:
>>> In comp.lang.javascript message <1788183.L...@PointedEars.de>,
>>> Sun, 18 Nov 2012 01:56:31, Thomas 'PointedEars' Lahn
>>> <Point...@web.de> posted:
>>>
>>>> Number(myString) will return NaN if there are any non-digits and non-dots in
>>>> myString, ignoring leading whitespace.
>>>
>>> Trebly untrue, even disregarding the omissions of "decimal" and of
>>> trailing whitespace.
>>
>> On IE 9 at least, PE seems to be correct:
>>
>> var myString = "33a";
>> window.alert(Number(myString)); // alerts NaN
>>
>> myString = "3 3";
>> window.alert(Number(myString)); // alerts NaN
>>
>> myString = "33-";
>> window.alert(Number(myString)); // alerts NaN
>>
>> myString = " 33";
>> window.alert(Number(myString)); // alerts 33
>>
>> myString = "33 ";
>> window.alert(Number(myString)); // alerts 33
>
>
> You are merely proving that some non-digit non-dot characters will
> result in NaN, which is not the same thing. To prove the quoted
> assertion, you need to consider all possible non-s in all possible
> places. But to disprove an "any" statement, a single failure suffices.

I have no idea what you are talking about: whether its PE's assertion;
my "proof" that PE was correct; your dislike of tinny audio; your
dislike for PE... what?

Andrew Poulos
0 new messages