Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
165 views
in Technique[技术] by (71.8m points)

Javascript Number formating winthout numbers after comma

I am trying this script to calculate and prit the result in a form input readonly. While this works fine i need the numbers to be shown like this: 2.000.000 instead of 2.000.000.00. I need those 2 zeros at the end gone. I googled it but all I find are sollutions with the comma intact. As this script is very basic and simple i hope that for my problem there is a very simple and basic sollution.

<script>
        getPrice = function() {
            var numVal1 = Number(document.getElementById("price").value);
            var numVal2 = Number(document.getElementById("discount").value) ;
            var totalValue = numVal1 * numVal2
            document.getElementById("total").value = totalValue.toFixed(2).replace(/d(?=(d{3})+.)/g, '$&.');
           
        }
    </script>
question from:https://stackoverflow.com/questions/65893385/javascript-number-formating-winthout-numbers-after-comma

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Have you tried this :

totalValue.toFixed(2).replace(/d(?=(d{3})+.)/g, '$&.').slice(0, -3)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...