简体   繁体   English

使计算包括小数

[英]Make calculation include decimals

I am new to Javascript and I got most of this code from another Q&A on this site worked great! 我是Java语言的新手,我从此站点上的另一个问答获得了大部分代码,效果很好! Only problem is that the code doesn't count the decimals! 唯一的问题是代码不计算小数! How do I get this code to count the decimals? 如何获得此代码来计算小数?

<table> <tr class=r1> 
<th class=d1>1-24</th> 
<th class=d2>25-49</th> 
<th class=d2>50-99</th> 
<th class=d2>100-499</th> 
<th class=d2>500 = Offert</th> 
        <tr class=r1> 
        <td class=d1 id=A>99,00:-</td> 
        <td class=d2 id=B></td> 
        <td class=d2 id=C></td> 
        <td class=d2 id=D></td> 
        <td class=d2> </td> </tr>
</table>

That's the table I have to make a bracket price. 那就是我要定的价格表。

This is the Javascript: 这是Javascript:

(function(){
// put all your JavaScript in a closure so you don't pollute the global namespace
  function extract_float_from_price ( price ) {
    return parseFloat( price.replace(/\:*-/,'') );
  }

  function evaluate_savings ( ) {
    var A = extract_float_from_price( $('#A').text() );

    $('#B').text( parseInt(A * 0.96 ) + ':-' );
        $('#C').text( parseInt(A * 0.92 ) + ':-' );
        $('#D').text( parseInt(A * 0.88 ) + ':-' );
  }

  $( evaluate_savings ); // binds to Dom Ready
})()

Please help me get the code to show decimals for a more exact price. 请帮助我获取显示小数的代码,以获取更准确的价格。

Use parseFloat rather than parseInt in the following statements 在以下语句中使用parseFloat而不是parseInt

    $('#B').text( parseFloat(A * 0.96 ) + ':-' );
    $('#C').text( parseFloat(A * 0.92 ) + ':-' );
    $('#D').text( parseFloat(A * 0.88 ) + ':-' );

BTW, it is great that some answer helped you do something, but you should also try to understand what the code does and how it does when you use it. 顺便说一句,一定的答案可以帮助您做一些事情,这是很好的,但是您也应该尝试了解代码的用途以及使用时的方式。

And, Java != Javascript 而且, Java!= Javascript

Update : 更新

To round a number use toFixed . 要舍入一个数字,请使用toFixed You might want to do: 您可能想要做:

$('#B').text( parseFloat(A * 0.96 ).toFixed(2) + ':-' );

and so on 等等

Something to learn immediately, so you get better responses in future, this is JavaScript not Java. 立即学习一些东西,以便将来获得更好的响应,这是JavaScript 而不是 Java。 They look similar, but are by no means the same. 它们看起来相似,但绝非相同。 There are plenty of discussions on the Internet describing the differences. 互联网上有大量讨论描述了这些差异。

In answer to your question, try the JavaScript function parseFloat instead of parseInt . 要回答您的问题,请尝试使用JavaScript函数parseFloat而不是parseInt

You need to do two things 你需要做两件事

  • use parseFloat 使用parseFloat
  • replace , with . 替换,. as . 作为。 is the comma separator in javascript. 是javascript中的逗号分隔符。

So, 所以,

// get the part of the text that should be parsed
var A = /[\d,.]+/.exec($('#A').text())[0];

// replace any , with .
A = A.replace(",",".")

// parse the string into a Number
A = A.parseFloat(A, 10);

// output the result
$('#B').text( (A * 0.96)  + ':-' );

should work for you 应该为你工作

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

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