SORU
9 HAZİRAN 2009, Salı


JavaScript string ve sayı dönüştürme

Nasıl JavaScript şunları yapabilir miyim?

  1. "1", "2", "3" "" . 123 içine bağlamak

  2. "123" 123

  3. 123 100 = 223 ekleyin

  4. Gizli 223"" . 223 içine

CEVAP
9 HAZİRAN 2009, Salı


parseInt aşina olmak istiyorum () toString().

Ve araci yararlı - typeof ne olduğunu bulmak için bir değişken bakmak olacak:

<script type="text/javascript">
    /**
     * print out the value and the type of the variable passed in
     */

    function printWithType(val) {
        document.write('<pre>');
        document.write(val);
        document.write(' ');
        document.writeln(typeof val);
        document.write('</pre>');
    }

    var a = "1", b = "2", c = "3", result;

    // Step (1) Concatenate "1", "2", "3" into "123"
    // - concatenation operator is just " ", as long
    //   as all the items are strings, this works
    result = a   b   c;
    printWithType(result); //123 string

    // - If they were not strings you could do
    result = a.toString()   b.toString()   c.toString();
    printWithType(result); // 123 string

    // Step (2) Convert "123" into 123
    result = parseInt(result,10);
    printWithType(result); // 123 number

    // Step (3) Add 123   100 = 223
    result = result   100;
    printWithType(result); // 223 number

    // Step (4) Convert 223 into "223"
    result = result.toString(); //
    printWithType(result); // 223 string

    // If you concatenate a number with a 
    // blank string, you get a string    
    result = result   "";
    printWithType(result); //223 string
</script>

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Ash100HD

    Ash100HD

    29 EKİM 2011
  • Charles Nesson

    Charles Ness

    27 NİSAN 2006
  • PlugResearch

    PlugResearch

    22 Mart 2006