sof-03
sof-03

Reputation: 2505

In Vue.js project, I can not get the correct multiplication result

In my vue.js project, the number * 100 get wrong number.

In my template:

<div class="res-show">
  总共折点: <span style="color:#ed3f14">{{ discount_point_total }}</span> <br>
  相当于折扣掉总价: <span>{{discount_point_total * 100}}%</span>
</div>

in my script:

export default{
    data(){
      return {
        discount_point_total:0.022
        ...

Upvotes: 0

Views: 147

Answers (3)

Jacob Goh
Jacob Goh

Reputation: 20845

alternatively, use toFixed

<span>{{ (discount_point_total*100).toFixed(2) }}%</span>

Upvotes: 1

Roy lin
Roy lin

Reputation: 66

Use toFixed {{ (0.022*100).toFixed(2) }}%

Note that the return value of this method is a string. Remember parseFloat when you use it. You need to know more about the foundation of ECMA Script.

Upvotes: 0

aircraft
aircraft

Reputation: 26924

The float point number precision lost, you can use this method to avoid the issue:

<span>{{ Math.round(discount_point_total * 100 * 100) / 100 }}%</span>

Upvotes: 1

Related Questions