2017-06-19 12 views
0

変数を取り、2つの演算を計算し、両方の答えを画面に表示するスクリプトを一緒に書きました。JavaScriptの準備ができており、数値結果にカンマを挿入する必要があります

結果が1,000を超えると、両方の "生成された"回答にカンマを配置するスクリプトが必要です。 (HTMLに表示されるデフォルト値にはカンマが既にあります)

しかし、結果に小数点の後に小数点が表示される必要はありません。

ここに私のJavaScriptといくつかの付属HTMLです...

function return_selected(currentID) { 
 
    var savings = document.getElementById(currentID); 
 
    var valueCalc = savings.options[savings.selectedIndex].value; 
 
    return valueCalc; 
 
} 
 

 
// computing potential at 3% 
 
function firstCalc() { 
 
    var calcPrice1 = return_selected('soldfor') * .03 - 595; 
 
    document.getElementById('savings3').innerHTML = "$" + calcPrice1; 
 
    return calcPrice1; 
 
} 
 

 
// computing potential at 6% 
 
function secondCalc() { 
 
    var calcPrice2 = return_selected('soldfor') * .06 - 595; 
 
    document.getElementById('savings6').innerHTML = "$" + calcPrice2; 
 
    return calcPrice2; 
 
} 
 

 
document.addEventListener("DOMContentLoaded", firstCalc); 
 
document.addEventListener("DOMContentLoaded", secondCalc); 
 
// should listen for all select option 
 
document.addEventListener("change", firstCalc); 
 
document.addEventListener("change", secondCalc);
<!--HTML Routine to DISPLAY COMPUTATIONS -->** 
 

 
<div class="box"> 
 
    <div class="content"> 
 
    <p>Start with this number 
 
     <select name="" id="soldfor"> 
 
      <option value="100000">$100,000</option> 
 
      <option value="200000">$200,000</option> 
 
      <option value="300000">$300,000</option> 
 
      <option value="400000" selected>$400,000</option> 
 
      <option value="450000">$450,000</option> 
 
      </select> 
 
    </p>Compute a new number <span id="savings3">$11,405</span> 
 
    <p> 
 
     <p>and compute another number 
 
     <span id="savings6">$23,405</span> 
 
    </div> 
 
</div>

+1

現代のjavascript内の数値は '.toLocaleString()'メソッドとMathオブジェクトは、常に –

答えて

0
function addComma(val) { 
    while (/(\d+)(\d{3})/.test(val.toString())) { 
     val = val.toString().replace(/(\d+)(\d{3})/, '$1' + ',' + '$2'); 
    } 
    return val; 
} 

var calcPrice1 = 100000 * .03 - 595; 
document.getElementById('savings3').innerHTML = "$" + addComma(Math.trunc(calcPrice1)); 

var calcPrice2 = 100000 * .06 - 595; 
document.getElementById('savings6').innerHTML = "$" + addComma(Math.trunc(calcPrice2)); 

それはあなたのために動作します。

+0

本当にありがとうございまし必要に応じて小数点を削除するには、ラウンド/フロア/切り上げ機能を持っていていますこの新生児が厳しい状況を乗り越えるのを手伝ってくれた(誰にとっても)! – Ittledoo

0

Number.toLocaleString()を使用すると、ロケールの規則に従って番号をフォーマットできます。

var n1 = 1e5; 
var n2 = 1e6; 

console.log('n1:',n1.toLocaleString()); 
//=> n1: 100,000 
console.log('n2:',n2.toLocaleString()); 
//=> n2: 1,000,000 

Number.prototype.toLocaleString() @ MDNを参照してください。

0
if (!Number.prototype.toMoney) { 
    Number.prototype.toMoney = function() {     
     return this.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); 
    }; 
} 

console.log((4500000.00).toMoney()); // 4,500,000 

OR

var format = function (n) {     
    return n.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); 
}; 

console.log(format(10500)); // 10,500 
関連する問題