Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 3 123 LastLast
Results 1 to 15 of 32

Thread: toString()

  1. #1
    New to the CF scene
    Join Date
    Aug 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    toString()

    I'm average when it comes to math so I'm having trouble understanding the concept of how you convert an integer to a string, and what would be the best outcome? Can someone help me understand the toString() method please?

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,083
    Thanks
    38
    Thanked 498 Times in 492 Posts
    Not much to it. See results of script below and look at comments.
    Code:
    <script type="text/javascript">
    var n1 = 3;  // a number
    var n2 = 4;  // another number
    
    var s1 = n1.toString();  // a string
    var s2 = n2.toString();  // another string
    
    var nadd = n1+n2;  // add numbers
    var nsub = n1-n2;  // subtract numbers
    var nmul = n1*n2;  // multiply numbers
    var ndiv = n1/n2;  // divide numbers
    
    var s = s1+s2;     // add (concatenate) strings
    // cannot subtract, multiply or divide any two strings
    // string would need to be converted to numbers before math can be done
    // stings to numbers by Number(), parseInt(), parseFloat(); stringNumber * 1;
    
    alert(nadd+'\n'+nsub+'\n'+nmul+'\n'+ndiv+'\n'+s);
    </script>

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,436
    Thanks
    75
    Thanked 4,372 Times in 4,337 Posts
    Are you talking about how to *IMPLEMENT* toString()???

    If so, are you talking about JavaSCRIPT or Java?? Because this sounds more like an exercise for a class in Java than for one in JavaSCRIPT. (This forum is for JavaSCRIPT. Almost the only similarity to Java is the first 4 letters of their names.)

    If you are indeed asking how to implement toString()--that is, write your own as if the method didn't already exist--then do let us know. And confirm whether you are talking JavaScript or not.

  • #4
    New to the CF scene
    Join Date
    Aug 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ah, no this is completely for JavaScript. I'm just 'thrown off I guess you could say' by changing a number into a word, if that's how toString() works. Or do I have the understanding wrong?

    What I mean by that is say my small script is

    var one = 1;
    var changed = one.toString();

    alert(changed);

    what will changed return and why??

    So, from jmrker's example above, what I get out of it is it just changes the datatype itself and not the value, which is why the concatanation was possible and it didnt use math when using the + operator, at least thast what I got out of his code example. Is that what it is? so since you passed the two 'number' variables into the toString() method, we can now execute methods and treat these variables as if they are now datatype strings? Is it that simple? I'm probably just overthinking this.
    Last edited by clear; 08-01-2011 at 04:43 AM.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,436
    Thanks
    75
    Thanked 4,372 Times in 4,337 Posts
    Well, yes and no.

    Changing the datatype isn't at all as simple as you might think.

    The internal representation of, say, the number 101 might be something like
    Code:
    00000000 00000000 00000000 001100101
    That's a 4-byte integer. Or it might be stored as
    Code:
    01000001 10010100 00000000 00000000
    that's a 4-byte floating point number (if I got my bits in the right spots...it's been at least a dozen years since I played with floating point at the bit level).

    But that number *AS A STRING* would be
    Code:
    00110001 00110000 00110001
    Which is actually THREE SEPARATE ASCII CHARACTERS. The character "1", the character "0", and then the character "1". (And, actually, each character is likely 2 bytes long, not one as shown, because JS is a Unicode language...but let's not get into that.)

    The point is, as a *string*, there is nothing to distinguish "101" from "aba" or "$?X" or any other three ASCII characters. Excepting, of course, that actual values of the characters.

    So you wouldn't try to do
    Code:
    "Axe" * "Woof"
    (in an attempt to multiply two strings).

    And if JavaScript weren't such a forgiving language (a.k.a. an "untyped" language), then you wouldn't be able to do
    Code:
    "101" * "371"
    As it happens, JMrker didn't tell the whole truth: If you attempt to do that with JavaScript, it *WILL* work and you *WILL* end up multiplying two numbers, just as if you had coded
    Code:
    101 * 371
    That's because JavaScript (almost, but not quite, alone among common langusges--see also PHP) *will* coerce a string into a number if it thinks that doing so will allow it to perform a mathematical operation.

    BUT...that is not the case with
    Code:
    "101" + "371"
    where the result will be
    Code:
    "101371"
    and that's because of an idiocy perpetrated in the design of Java many moons ago (and copied by JavaScript) wherein they decided to use the + operator for both mathematical addition and string concatenation. So if used with strings, you get concatenation. (PHP isn't my favorite language, but they got it right: PHP uses a period for string concatenation. Personally, I would have rather seen an operator that is otherwise unused, such as @ or # or $, but oh well...)

  • #6
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,083
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Arrow

    As it happens, JMrker didn't tell the whole truth: If you attempt to do that with JavaScript, it *WILL* work and you *WILL* end up multiplying two numbers, just as if you had coded
    Code:

    101 * 371

    That's because JavaScript (almost, but not quite, alone among common langusges--see also PHP) *will* coerce a string into a number if it thinks that doing so will allow it to perform a mathematical operation.

    BUT...that is not the case with
    Code:

    "101" + "371"

    where the result will be
    Code:

    "101371"
    The reason I did not "tell the whole truth" is because I find it easier to remember the rules
    rather than the rules AND then the exceptions to the rules.

    For example, while
    var n = '101' * "371'
    might work very nicely for some math calculations,
    if the user has entered the '101' as '1O1' instead,
    then when they get an error on the 'O'
    because it looks similar to the '0'
    they would not need to remember the exceptions.
    Last edited by jmrker; 08-02-2011 at 01:06 AM.

  • #7
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,333
    Thanks
    11
    Thanked 587 Times in 568 Posts
    you can use + to coerce a number:

    Code:
    "101" + "371" == "101371";
    
    
    +"101" + +"371" == 472
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,436
    Thanks
    75
    Thanked 4,372 Times in 4,337 Posts
    Oh, I 100% agree with you, JMrker! I *NEVER* rely upon JavaScript converting strings to numbers for me.

    I *always* do something like:
    Code:
    var n = Number( someString );
    if ( isNaN(n) ) { alert("doofus...I want a number"); }
    ...
    (Or parseInt if I want to insist that the value be an integer. Or ... )

    I think it is a real hack to code stuff like
    Code:
    var n = 1 * someString;
    I know it works. I just don't like it.

  • #9
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,333
    Thanks
    11
    Thanked 587 Times in 568 Posts
    1* is messy, but Number() is several times slower...

    i like operational casters:
    Code:
    var n = +someString;
    Code:
    var n = +new Date;
    Code:
    var b = !!someString;
    strings are slightly more complicated:
    Code:
    var s = ""+someString;
    Code:
    var s = ""+new Date;


    try to avoid the performance overhead of calling type functions (String, Number, Boolean), especially in loops and animation code.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #10
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    Quote Originally Posted by rnd me View Post
    try to avoid the performance overhead of calling type functions (String, Number, Boolean), especially in loops and animation code.
    I use the Number object all the time to convert a string to a number without any noticeable increase in execution time or any other overhead.

    Whenever someone says something is faster or slower, I usually ask them to quantify how much faster or slower because if the order of magnitude is in nano or even micro-seconds I couldn't care less about the increase in execution time.

  • #11
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,982
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    Quote Originally Posted by bullant View Post
    I use the Number object all the time to convert a string to a number without any noticeable increase in execution time or any other overhead.

    Whenever someone says something is faster or slower, I usually ask them to quantify how much faster or slower because if the order of magnitude is in nano or even micro-seconds I couldn't care less about the increase in execution time.
    I have to say that I fully agree with these remarks. But I do not really understand why Old Pedant objects to the efficient var n = 1 * someString;

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #12
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    he didn't say he objects to it. He said he doesn't like it.

    I don't like it either. It looks klunky and is a "poor man's" way of doing it imo.

  • #13
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,982
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    As you so often say, you are entitled to your opinion.

    Why is var n = 1 * someString;
    somehow less acceptable than
    var n = +someString;

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #14
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    Quote Originally Posted by Philip M View Post
    As you so often say, you are entitled to your opinion.
    yep, and I posted why I agree with Old Pedant

    Quote Originally Posted by Philip M View Post
    Why is var n = 1 * someString;
    somehow less acceptable than
    var n = +someString;
    less acceptable to who?
    Last edited by bullant; 08-02-2011 at 02:30 PM.

  • #15
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,083
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Lightbulb Just an observation ...

    Just an observation ...
    I'm not sure the operational casters idea by 'rnd me' are beneficial to me:
    Code:
    <script type="text/javascript">
    var s1 = '123';
    var s2 = '456';
    var n1 = +s1;
    var n2 = +s2;
    n = n1 + n2;
    sa = +s1 + +s2;
    ss = +s1 - +s2;
    sm = +s1 * +s2;
    sd = +s1 / +s2;
    
    alert(n+'\n\n'+sa+'\n'+ss+'\n'+sm+'\n'+sd);
    alert('Still concatenates: '+ +'123'+ +s1+'\nbut with () works OK: '+ (+'123'+ +s1));
    </script>
    While the operations do work to cast the string to a number,
    IMO, all the extra '+' characters in a row looks confusing to the casual coder.

    For example, with a slip of the space bar and the expression
    var n = +s1 + +s2;
    becomes:
    var n = +s1++s2;


  •  
    Page 1 of 3 123 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •