Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
how to remove trailing decimal or decimal 0 of a string?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  24 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
skrite  
View profile  
 More options Nov 16 2012, 12:12 pm
Newsgroups: comp.lang.javascript
From: skrite <sh...@skrite.net>
Date: Fri, 16 Nov 2012 09:12:49 -0800 (PST)
Local: Fri, Nov 16 2012 12:12 pm
Subject: how to remove trailing decimal or decimal 0 of a string?
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.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Andrew Poulos  
View profile  
 More options Nov 16 2012, 3:10 pm
Newsgroups: comp.lang.javascript
From: Andrew Poulos <ap_p...@hotmail.com>
Date: Sat, 17 Nov 2012 07:10:42 +1100
Local: Fri, Nov 16 2012 3:10 pm
Subject: Re: how to remove trailing decimal or decimal 0 of a string?
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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Andrew Poulos  
View profile  
 More options Nov 16 2012, 9:52 pm
Newsgroups: comp.lang.javascript
From: Andrew Poulos <ap_p...@hotmail.com>
Date: Sat, 17 Nov 2012 13:51:54 +1100
Local: Fri, Nov 16 2012 9:51 pm
Subject: Re: how to remove trailing decimal or decimal 0 of a string?
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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Denis McMahon  
View profile  
 More options Nov 17 2012, 3:05 am
Newsgroups: comp.lang.javascript
From: Denis McMahon <denismfmcma...@gmail.com>
Date: Sat, 17 Nov 2012 08:05:17 +0000 (UTC)
Local: Sat, Nov 17 2012 3:05 am
Subject: Re: how to remove trailing decimal or decimal 0 of a string?

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Evertjan.  
View profile  
 More options Nov 17 2012, 3:24 am
Newsgroups: comp.lang.javascript
From: "Evertjan." <exxjxw.hannivo...@inter.nl.net>
Date: Sat, 17 Nov 2012 09:24:39 +0100
Local: Sat, Nov 17 2012 3:24 am
Subject: Re: how to remove trailing decimal or decimal 0 of a string?
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)


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Evertjan.  
View profile  
 More options Nov 17 2012, 3:29 am
Newsgroups: comp.lang.javascript
From: "Evertjan." <exxjxw.hannivo...@inter.nl.net>
Date: Sat, 17 Nov 2012 09:29:37 +0100
Local: Sat, Nov 17 2012 3:29 am
Subject: Re: how to remove trailing decimal or decimal 0 of a string?
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)


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Christoph Becker  
View profile  
 More options Nov 17 2012, 6:04 am
Newsgroups: comp.lang.javascript
From: Christoph Becker <cmbecke...@gmx.de>
Date: Sat, 17 Nov 2012 12:04:55 +0100
Local: Sat, Nov 17 2012 6:04 am
Subject: Re: how to remove trailing decimal or decimal 0 of a string?

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Thomas 'PointedEars' Lahn  
View profile  
 More options Nov 17 2012, 9:37 am
Newsgroups: comp.lang.javascript
Followup-To: comp.lang.javascript
From: Thomas 'PointedEars' Lahn <PointedE...@web.de>
Date: Sat, 17 Nov 2012 15:37:02 +0100
Local: Sat, Nov 17 2012 9:37 am
Subject: Re: how to remove trailing decimal or decimal 0 of a string?

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$8300d...@news.demon.co.uk>


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
BootNic  
View profile  
 More options Nov 17 2012, 11:28 am
Newsgroups: comp.lang.javascript
From: BootNic <bootnic.bou...@gmail.com>
Date: Sat, 17 Nov 2012 11:28:26 -0500
Local: Sat, Nov 17 2012 11:28 am
Subject: Re: how to remove trailing decimal or decimal 0 of a string?

In article <e89247f7-aa7b-4c64-9de5-265ab78a5336@googlegroups.com>,

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:

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
< 1K Download

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Andrew Poulos  
View profile  
 More options Nov 17 2012, 4:28 pm
Newsgroups: comp.lang.javascript
From: Andrew Poulos <ap_p...@hotmail.com>
Date: Sun, 18 Nov 2012 08:28:11 +1100
Local: Sat, Nov 17 2012 4:28 pm
Subject: Re: how to remove trailing decimal or decimal 0 of a string?
On 18/11/2012 1:37 AM, Thomas 'PointedEars' Lahn wrote:

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dr J R Stockton  
View profile  
 More options Nov 17 2012, 4:08 pm
Newsgroups: comp.lang.javascript
From: Dr J R Stockton <reply1...@merlyn.demon.co.uk.invalid>
Date: Sat, 17 Nov 2012 21:08:49 +0000
Local: Sat, Nov 17 2012 4:08 pm
Subject: Re: how to remove trailing decimal or decimal 0 of a string?
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:

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

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 ?.?.Stock...@physics.org
  Web  <http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, and links.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Thomas 'PointedEars' Lahn  
View profile  
 More options Nov 17 2012, 7:02 pm
Newsgroups: comp.lang.javascript
Followup-To: comp.lang.javascript
From: Thomas 'PointedEars' Lahn <PointedE...@web.de>
Date: Sun, 18 Nov 2012 01:02:06 +0100
Local: Sat, Nov 17 2012 7:02 pm
Subject: Re: how to remove trailing decimal or decimal 0 of a string?

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.

     ^^^^^^^^^^^^^^^^^^^^^^^

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>


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Thomas 'PointedEars' Lahn  
View profile  
 More options Nov 17 2012, 7:56 pm
Newsgroups: comp.lang.javascript
Followup-To: comp.lang.javascript
From: Thomas 'PointedEars' Lahn <PointedE...@web.de>
Date: Sun, 18 Nov 2012 01:56:31 +0100
Local: Sat, Nov 17 2012 7:56 pm
Subject: Re: how to remove trailing decimal or decimal 0 of a string?

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Andrew Poulos  
View profile  
 More options Nov 17 2012, 11:40 pm
Newsgroups: comp.lang.javascript
From: Andrew Poulos <ap_p...@hotmail.com>
Date: Sun, 18 Nov 2012 15:40:17 +1100
Local: Sat, Nov 17 2012 11:40 pm
Subject: Re: how to remove trailing decimal or decimal 0 of a string?
On 18/11/2012 11:02 AM, Thomas 'PointedEars' Lahn wrote:

Dang, I thought I had one over on you :-)

Andrew Poulos


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dr J R Stockton  
View profile  
 More options Nov 19 2012, 2:53 pm
Newsgroups: comp.lang.javascript
From: Dr J R Stockton <reply1...@merlyn.demon.co.uk.invalid>
Date: Mon, 19 Nov 2012 19:53:25 +0000
Local: Mon, Nov 19 2012 2:53 pm
Subject: Re: how to remove trailing decimal or decimal 0 of a string?
In comp.lang.javascript message <1788183.LAg6v8g...@PointedEars.de>,
Sun, 18 Nov 2012 01:56:31, Thomas 'PointedEars' Lahn
<PointedE...@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>.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Andrew Poulos  
View profile  
 More options Nov 19 2012, 7:04 pm
Newsgroups: comp.lang.javascript
From: Andrew Poulos <ap_p...@hotmail.com>
Date: Tue, 20 Nov 2012 11:03:55 +1100
Local: Mon, Nov 19 2012 7:03 pm
Subject: Re: how to remove trailing decimal or decimal 0 of a string?
On 20/11/2012 6:53 AM, Dr J R Stockton wrote:

> In comp.lang.javascript message <1788183.LAg6v8g...@PointedEars.de>,
> Sun, 18 Nov 2012 01:56:31, Thomas 'PointedEars' Lahn
> <PointedE...@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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Thomas 'PointedEars' Lahn  
View profile  
 More options Nov 19 2012, 7:05 pm
Newsgroups: comp.lang.javascript
Followup-To: comp.lang.javascript
From: Thomas 'PointedEars' Lahn <PointedE...@web.de>
Date: Tue, 20 Nov 2012 01:04:40 +0100
Local: Mon, Nov 19 2012 7:04 pm
Subject: Re: how to remove trailing decimal or decimal 0 of a string?
Dr J R Stockton wrote:

> Thomas 'PointedEars' Lahn <PointedE...@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).


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Thomas 'PointedEars' Lahn  
View profile  
 More options Nov 19 2012, 7:50 pm
Newsgroups: comp.lang.javascript
Followup-To: comp.lang.javascript
From: Thomas 'PointedEars' Lahn <PointedE...@web.de>
Date: Tue, 20 Nov 2012 01:50:16 +0100
Local: Mon, Nov 19 2012 7:50 pm
Subject: Re: how to remove trailing decimal or decimal 0 of a string?

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Christoph Becker  
View profile  
 More options Nov 19 2012, 8:09 pm
Newsgroups: comp.lang.javascript
From: Christoph Becker <cmbecke...@gmx.de>
Date: Tue, 20 Nov 2012 02:10:31 +0100
Local: Mon, Nov 19 2012 8:10 pm
Subject: Re: how to remove trailing decimal or decimal 0 of a string?

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Thomas 'PointedEars' Lahn  
View profile  
 More options Nov 20 2012, 4:01 pm
Newsgroups: comp.lang.javascript
Followup-To: comp.lang.javascript
From: Thomas 'PointedEars' Lahn <PointedE...@web.de>
Date: Tue, 20 Nov 2012 21:58:58 +0100
Local: Tues, Nov 20 2012 3:58 pm
Subject: Re: how to remove trailing decimal or decimal 0 of a string?

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

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$8300d...@news.demon.co.uk>


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dr J R Stockton  
View profile  
 More options Nov 21 2012, 1:44 pm
Newsgroups: comp.lang.javascript
From: Dr J R Stockton <reply1...@merlyn.demon.co.uk.invalid>
Date: Wed, 21 Nov 2012 18:44:14 +0000
Local: Wed, Nov 21 2012 1:44 pm
Subject: Re: how to remove trailing decimal or decimal 0 of a string?
In comp.lang.javascript message <1-ydnQHMtu9oVTfNnZ2dnUVZ_hednZ2d@westne
t.com.au>, Tue, 20 Nov 2012 11:03:55, Andrew Poulos
<ap_p...@hotmail.com> posted:

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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dr J R Stockton  
View profile  
 More options Nov 21 2012, 2:00 pm
Newsgroups: comp.lang.javascript
From: Dr J R Stockton <reply1...@merlyn.demon.co.uk.invalid>
Date: Wed, 21 Nov 2012 19:00:40 +0000
Local: Wed, Nov 21 2012 2:00 pm
Subject: Re: how to remove trailing decimal or decimal 0 of a string?
In comp.lang.javascript message <1538569.gxPoS1l...@PointedEars.de>,
Tue, 20 Nov 2012 01:04:40, Thomas 'PointedEars' Lahn
<PointedE...@web.de> posted:

>Dr J R Stockton wrote:

>> Thomas 'PointedEars' Lahn <PointedE...@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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dr J R Stockton  
View profile  
 More options Nov 21 2012, 2:09 pm
Newsgroups: comp.lang.javascript
From: Dr J R Stockton <reply1...@merlyn.demon.co.uk.invalid>
Date: Wed, 21 Nov 2012 19:09:22 +0000
Local: Wed, Nov 21 2012 2:09 pm
Subject: Re: how to remove trailing decimal or decimal 0 of a string?
In comp.lang.javascript message <k8el8d$l7...@speranza.aioe.org>, Tue,
20 Nov 2012 02:10:31, Christoph Becker <cmbecke...@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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Andrew Poulos  
View profile  
 More options Nov 21 2012, 10:07 pm
Newsgroups: comp.lang.javascript
From: Andrew Poulos <ap_p...@hotmail.com>
Date: Thu, 22 Nov 2012 14:07:00 +1100
Subject: Re: how to remove trailing decimal or decimal 0 of a string?
On 22/11/2012 5:44 AM, Dr J R Stockton wrote:

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »