3ケタ毎にカンマ,を入れて表示する関数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
function p(a){ console.log(a); } function getKeta(n){ if(n<0){ n *= -1; } var keta = 0; if(0===n){ return 1; } while(0<n){ keta++; n = Math.floor(n / 10); } return keta; } function intToArray(n){ if(n<0){ n *= -1; } var keta = getKeta(n); var a = []; for(var i=0; i<keta; i++){ var t = n % 10; n = Math.floor(n / 10); a[keta-1-i] = t; } return a; } function swap(a, l, r){ var t = a[l]; a[l] = a[r]; a[r] = t; } function reverse(a){ var len = a.length; var half = Math.floor(len/2); for(var i=0; i<half; i++){ swap(a, i, len-1-i); } } function dispWithComma(n) { var isNegative = false; if(n<0){ n *= -1; isNegative = true; } var a = intToArray(n); var b = []; var cnt = 0; for(var i=0; i<a.length; i++){ var v = a[a.length-1-i]; if(i !== 0 && 0===i%3){ b[cnt++] = ","; } b[cnt++] = v; } reverse(b); var s = ""; if(isNegative){ s = "-" + s; } for(var v of b){ s += v; } p(s); } // ユロ円レート const euroToYen = 129.294549; function main(){ // 5000まんゆーろ var euro = 50000000; var yen = Math.floor(euroToYen * euro); dispWithComma(yen); yen = -yen; dispWithComma(yen); } main(); |
1 2 |
6,464,727,449 -6,464,727,449 |