Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #17267 > unrolled thread
| Started by | skrite <shawn@skrite.net> |
|---|---|
| First post | 2012-11-16 09:12 -0800 |
| Last post | 2012-11-17 21:08 +0000 |
| Articles | 20 on this page of 24 — 9 participants |
Back to article view | Back to comp.lang.javascript
how to remove trailing decimal or decimal 0 of a string? skrite <shawn@skrite.net> - 2012-11-16 09:12 -0800
Re: how to remove trailing decimal or decimal 0 of a string? Andrew Poulos <ap_prog@hotmail.com> - 2012-11-17 07:10 +1100
Re: how to remove trailing decimal or decimal 0 of a string? Andrew Poulos <ap_prog@hotmail.com> - 2012-11-17 13:51 +1100
Re: how to remove trailing decimal or decimal 0 of a string? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-11-17 15:37 +0100
Re: how to remove trailing decimal or decimal 0 of a string? Andrew Poulos <ap_prog@hotmail.com> - 2012-11-18 08:28 +1100
Re: how to remove trailing decimal or decimal 0 of a string? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-11-18 01:02 +0100
Re: how to remove trailing decimal or decimal 0 of a string? Andrew Poulos <ap_prog@hotmail.com> - 2012-11-18 15:40 +1100
Re: how to remove trailing decimal or decimal 0 of a string? Denis McMahon <denismfmcmahon@gmail.com> - 2012-11-17 08:05 +0000
Re: how to remove trailing decimal or decimal 0 of a string? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2012-11-17 09:24 +0100
Re: how to remove trailing decimal or decimal 0 of a string? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2012-11-17 09:29 +0100
Re: how to remove trailing decimal or decimal 0 of a string? Christoph Becker <cmbecker69@gmx.de> - 2012-11-17 12:04 +0100
Re: how to remove trailing decimal or decimal 0 of a string? BootNic <bootnic.bounce@gmail.com> - 2012-11-17 11:28 -0500
Re: how to remove trailing decimal or decimal 0 of a string? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-11-18 01:56 +0100
Re: how to remove trailing decimal or decimal 0 of a string? Dr J R Stockton <reply1247@merlyn.demon.co.uk.invalid> - 2012-11-19 19:53 +0000
Re: how to remove trailing decimal or decimal 0 of a string? Andrew Poulos <ap_prog@hotmail.com> - 2012-11-20 11:03 +1100
Re: how to remove trailing decimal or decimal 0 of a string? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-11-20 01:50 +0100
Re: how to remove trailing decimal or decimal 0 of a string? Dr J R Stockton <reply1247@merlyn.demon.co.uk.invalid> - 2012-11-21 18:44 +0000
Re: how to remove trailing decimal or decimal 0 of a string? Andrew Poulos <ap_prog@hotmail.com> - 2012-11-22 14:07 +1100
Re: how to remove trailing decimal or decimal 0 of a string? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-11-20 01:04 +0100
Re: how to remove trailing decimal or decimal 0 of a string? Dr J R Stockton <reply1247@merlyn.demon.co.uk.invalid> - 2012-11-21 19:00 +0000
Re: how to remove trailing decimal or decimal 0 of a string? Christoph Becker <cmbecker69@gmx.de> - 2012-11-20 02:10 +0100
Re: how to remove trailing decimal or decimal 0 of a string? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-11-20 21:58 +0100
Re: how to remove trailing decimal or decimal 0 of a string? Dr J R Stockton <reply1247@merlyn.demon.co.uk.invalid> - 2012-11-21 19:09 +0000
Re: how to remove trailing decimal or decimal 0 of a string? Dr J R Stockton <reply1246@merlyn.demon.co.uk.invalid> - 2012-11-17 21:08 +0000
Page 1 of 2 [1] 2 Next page →
| From | skrite <shawn@skrite.net> |
|---|---|
| Date | 2012-11-16 09:12 -0800 |
| Subject | how to remove trailing decimal or decimal 0 of a string? |
| Message-ID | <e89247f7-aa7b-4c64-9de5-265ab78a5336@googlegroups.com> |
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.
[toc] | [next] | [standalone]
| From | Andrew Poulos <ap_prog@hotmail.com> |
|---|---|
| Date | 2012-11-17 07:10 +1100 |
| Message-ID | <KKadnc89ecxSADvNnZ2dnUVZ_qydnZ2d@westnet.com.au> |
| In reply to | #17267 |
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); var f = parseFloat(s); s = (i == f ? i : f) + ""; Andrew Poulos
[toc] | [prev] | [next] | [standalone]
| From | Andrew Poulos <ap_prog@hotmail.com> |
|---|---|
| Date | 2012-11-17 13:51 +1100 |
| Message-ID | <VMKdnaWc0KNKZjvNnZ2dnUVZ_q6dnZ2d@westnet.com.au> |
| In reply to | #17268 |
On 17/11/2012 7:10 AM, 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); > var f = parseFloat(s); > > s = (i == f ? i : f) + ""; Or maybe s = (f % 1 ? f : s) + ""; Andrew Poulos
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2012-11-17 15:37 +0100 |
| Message-ID | <1620256.BhNbSm6C5H@PointedEars.de> |
| In reply to | #17268 |
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$8300dec7@news.demon.co.uk>
[toc] | [prev] | [next] | [standalone]
| From | Andrew Poulos <ap_prog@hotmail.com> |
|---|---|
| Date | 2012-11-18 08:28 +1100 |
| Message-ID | <2KWdnfGCg9LknDXNnZ2dnUVZ_o2dnZ2d@westnet.com.au> |
| In reply to | #17279 |
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
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2012-11-18 01:02 +0100 |
| Message-ID | <1495886.W2LQtbraro@PointedEars.de> |
| In reply to | #17283 |
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:Xns9FB6521286DB8invalidcom@94.75.214.39>
[toc] | [prev] | [next] | [standalone]
| From | Andrew Poulos <ap_prog@hotmail.com> |
|---|---|
| Date | 2012-11-18 15:40 +1100 |
| Message-ID | <pNadnb6l4s0j-zXNnZ2dnUVZ_rmdnZ2d@westnet.com.au> |
| In reply to | #17286 |
On 18/11/2012 11:02 AM, Thomas 'PointedEars' Lahn wrote:
> 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'.
Dang, I thought I had one over on you :-)
Andrew Poulos
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2012-11-17 08:05 +0000 |
| Message-ID | <k87gft$gnb$2@dont-email.me> |
| In reply to | #17267 |
On Fri, 16 Nov 2012 09:12:49 -0800, 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.
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
[toc] | [prev] | [next] | [standalone]
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
|---|---|
| Date | 2012-11-17 09:24 +0100 |
| Message-ID | <XnsA10E5FBB759EEeejj99@194.109.133.133> |
| In reply to | #17267 |
skrite wrote on 16 nov 2012 in comp.lang.javascript: > 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. 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)
[toc] | [prev] | [next] | [standalone]
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
|---|---|
| Date | 2012-11-17 09:29 +0100 |
| Message-ID | <XnsA10E609365E2Feejj99@194.109.133.133> |
| In reply to | #17273 |
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*$/,''); -- Evertjan. The Netherlands. (Please change the x'es to dots in my emailaddress)
[toc] | [prev] | [next] | [standalone]
| From | Christoph Becker <cmbecker69@gmx.de> |
|---|---|
| Date | 2012-11-17 12:04 +0100 |
| Message-ID | <k87r0i$dam$1@speranza.aioe.org> |
| In reply to | #17267 |
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. 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
[toc] | [prev] | [next] | [standalone]
| From | BootNic <bootnic.bounce@gmail.com> |
|---|---|
| Date | 2012-11-17 11:28 -0500 |
| Message-ID | <20121117112826.00530fe7@bootnic.server> |
| In reply to | #17267 |
[Multipart message — attachments visible in raw view] — view raw
In article <e89247f7-aa7b-4c64-9de5-265ab78a5336@googlegroups.com>, skrite <shawn@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: 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.*
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2012-11-18 01:56 +0100 |
| Message-ID | <1788183.LAg6v8g3if@PointedEars.de> |
| In reply to | #17282 |
BootNic wrote: > skrite <shawn@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
[toc] | [prev] | [next] | [standalone]
| From | Dr J R Stockton <reply1247@merlyn.demon.co.uk.invalid> |
|---|---|
| Date | 2012-11-19 19:53 +0000 |
| Message-ID | <pLrc5UI14oqQFwuP@invalid.uk.co.demon.merlyn.invalid> |
| In reply to | #17287 |
In comp.lang.javascript message <1788183.LAg6v8g3if@PointedEars.de>, Sun, 18 Nov 2012 01:56:31, Thomas 'PointedEars' Lahn <PointedEars@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>.
[toc] | [prev] | [next] | [standalone]
| From | Andrew Poulos <ap_prog@hotmail.com> |
|---|---|
| Date | 2012-11-20 11:03 +1100 |
| Message-ID | <1-ydnQHMtu9oVTfNnZ2dnUVZ_hednZ2d@westnet.com.au> |
| In reply to | #17310 |
On 20/11/2012 6:53 AM, Dr J R Stockton wrote: > In comp.lang.javascript message <1788183.LAg6v8g3if@PointedEars.de>, > Sun, 18 Nov 2012 01:56:31, Thomas 'PointedEars' Lahn > <PointedEars@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
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2012-11-20 01:50 +0100 |
| Message-ID | <2212038.t9gtbIhNDI@PointedEars.de> |
| In reply to | #17312 |
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.)
[toc] | [prev] | [next] | [standalone]
| From | Dr J R Stockton <reply1247@merlyn.demon.co.uk.invalid> |
|---|---|
| Date | 2012-11-21 18:44 +0000 |
| Message-ID | <WCYDLEE+DSrQFwgP@invalid.uk.co.demon.merlyn.invalid> |
| In reply to | #17312 |
In comp.lang.javascript message <1-ydnQHMtu9oVTfNnZ2dnUVZ_hednZ2d@westne t.com.au>, Tue, 20 Nov 2012 11:03:55, Andrew Poulos <ap_prog@hotmail.com> posted: >On 20/11/2012 6:53 AM, Dr J R Stockton wrote: >> In comp.lang.javascript message <1788183.LAg6v8g3if@PointedEars.de>, >> Sun, 18 Nov 2012 01:56:31, Thomas 'PointedEars' Lahn >> <PointedEars@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.
[toc] | [prev] | [next] | [standalone]
| From | Andrew Poulos <ap_prog@hotmail.com> |
|---|---|
| Date | 2012-11-22 14:07 +1100 |
| Message-ID | <xv-dnQ03dIVDCzDNnZ2dnUVZ_q-dnZ2d@westnet.com.au> |
| In reply to | #17327 |
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_prog@hotmail.com> posted: > >> On 20/11/2012 6:53 AM, Dr J R Stockton wrote: >>> In comp.lang.javascript message <1788183.LAg6v8g3if@PointedEars.de>, >>> Sun, 18 Nov 2012 01:56:31, Thomas 'PointedEars' Lahn >>> <PointedEars@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
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2012-11-20 01:04 +0100 |
| Message-ID | <1538569.gxPoS1l1Lc@PointedEars.de> |
| In reply to | #17310 |
Dr J R Stockton wrote:
> Thomas 'PointedEars' Lahn <PointedEars@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).
[toc] | [prev] | [next] | [standalone]
| From | Dr J R Stockton <reply1247@merlyn.demon.co.uk.invalid> |
|---|---|
| Date | 2012-11-21 19:00 +0000 |
| Message-ID | <QixCTeFYTSrQFwj7@invalid.uk.co.demon.merlyn.invalid> |
| In reply to | #17313 |
In comp.lang.javascript message <1538569.gxPoS1l1Lc@PointedEars.de>,
Tue, 20 Nov 2012 01:04:40, Thomas 'PointedEars' Lahn
<PointedEars@web.de> posted:
>Dr J R Stockton wrote:
>
>> Thomas 'PointedEars' Lahn <PointedEars@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.
[toc] | [prev] | [next] | [standalone]
Page 1 of 2 [1] 2 Next page →
Back to top | Article view | comp.lang.javascript
csiph-web