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