简体   繁体   English

将数字格式添加为动画编号中的货币(10,000)

[英]Add number format as currency in animating number(10,000)

The amount is animating from start value to end value but i want to animate this amount number into formatted way. 该数量是从开始值到结束值的动画,但我想将此数量数字设置为格式化的动画。 Let suppose number start animating from 1000 to 10000 then it should be formatted as 10,000 假设数字从1000到10000开始动画,则应将其格式化为10,000

 function myCalculator() { $(".container2").show(); var start = 1000; var end = 10000; var duration = 2000; animateValue("value", start, end, duration).toLocaleString(); } function animateValue(id, start, end, duration) { var range = end - start; var current = start; var increment = end > start ? 10 : -10; var stepTime = Math.abs(Math.floor(duration / range)); var obj = document.getElementById(id); var timer = setInterval(function() { current += increment; obj.innerHTML = current; if (current == end) { clearInterval(timer); } }, stepTime); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <button type="button" onclick="myCalculator();">Click Here to See Animation</button> <div class="container2" style="display:none;"> Animating Amount $ <span id="value"></span> </div> 

I tried a lot but not found any luck. 我尝试了很多,但没有发现任何运气。 can anyone help me to fixed it? 谁能帮我修复它? Thanks in advance. 提前致谢。

You're calling toLocaleString() in the wrong place. 您在错误的位置调用了toLocaleString() You need to call it on the current variable within the animateValue function, not on the (non-existant) result of that function. 您需要在animateValue函数中的current变量上调用它,而不是在该函数的(不存在)结果上调用它。 Try this: 尝试这个:

 function myCalculator() { $(".container2").show(); var start = 1000; var end = 10000; var duration = 2000; animateValue("value", start, end, duration); // remove .toLocaleString() } function animateValue(id, start, end, duration) { var range = end - start; var current = start; var increment = end > start ? 10 : -10; var stepTime = Math.abs(Math.floor(duration / range)); var obj = document.getElementById(id); var timer = setInterval(function() { current += increment; obj.innerHTML = current.toLocaleString(); // add .toLocaleString() here if (current == end) { clearInterval(timer); } }, stepTime); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <button type="button" onclick="myCalculator();">Click Here to See Animation</button> <div class="container2" style="display:none;"> Animating Amount $ <span id="value"></span> </div> 

parseFloat(current).toLocaleString('en-US'); Do the trick 骗人

 function myCalculator() { $(".container2").show(); var start = 1000; var end = 10000; var duration = 2000; animateValue("value", start, end, duration); } function animateValue(id, start, end, duration) { var range = end - start; var current = start; var increment = end > start ? 10 : -10; var stepTime = Math.abs(Math.floor(duration / range)); var obj = document.getElementById(id); var timer = setInterval(function() { current += increment; obj.innerHTML = parseFloat(current).toLocaleString('en-US'); if (current == end) { clearInterval(timer); } }, stepTime); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <button type="button" onclick="myCalculator();">Click Here to See Animation</button> <div class="container2" style="display:none;"> Animating Amount $ <span id="value"></span> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM