收藏
回答

浮点小数如何解决的问题?

<!-- 交强险赔偿 -->
  <view class="result-row">
    <text class="result-cell">交强险赔偿</text>
    <block wx:for="{{vehicleIds}}" wx:key="id">
      <text class="result-cell">{{J[item-1] || 0}}</text>
    </block>
    <text class="result-cell">{{Jsum}}</text>
  </view>
  
  <!-- 车损险赔偿 -->
  <view class="result-row">
    <text class="result-cell">车损险赔偿</text>
    <block wx:for="{{vehicleIds}}" wx:key="id">
      <text class="result-cell">{{X[item-1] || 0}}</text>
    </block>
    <text class="result-cell">{{Xsum}}</text>
  </view>
  
  <!-- 商业三者险赔偿 -->
  <view class="result-row">
    <text class="result-cell">三者险赔偿</text>
    <block wx:for="{{vehicleIds}}" wx:key="id">
      <text class="result-cell">{{S[item-1] || 0}}</text>
    </block>
    <text class="result-cell">{{Ssum}}</text>
  </view>
  
  <!-- 总计 -->
  <view class="result-row total-row">
    <text class="result-cell">综合赔付金额</text>
    <block wx:for="{{vehicleIds}}" wx:key="id">
      <text class="result-cell">{{(J[item-1]) + (X[item-1]) +(S[item-1])}}</text>
  
    </block>
  
    <text class="result-cell">{{Jsum + Xsum + Ssum}}</text>
  </view>


              // 四舍五入保留两位小数
              const formattedJ = [this.roundToTwoDecimal(J1), this.roundToTwoDecimal(J2), this.roundToTwoDecimal(J3)];
              const formattedX = [this.roundToTwoDecimal(X1), this.roundToTwoDecimal(X2), this.roundToTwoDecimal(X3)];
              const formattedS = [this.roundToTwoDecimal(S1), this.roundToTwoDecimal(S2), this.roundToTwoDecimal(S3)];
              const formattedJsum = this.roundToTwoDecimal(Jsum);
              const formattedXsum = this.roundToTwoDecimal(Xsum);
              const formattedSsum = this.roundToTwoDecimal(Ssum);
              const formattedTotalCompensation = this.roundToTwoDecimal(totalCompensation);
              const formattedAsum = this.roundToTwoDecimal(Asum);
              const formattedBsum = this.roundToTwoDecimal(Bsum);
              const formattedCsum = this.roundToTwoDecimal(Csum);


              this.setData({
                  J: formattedJ,
                  X: formattedX,
                  S: formattedS,
                  Jsum: formattedJsum,
                  Xsum: formattedXsum,
                  Ssum: formattedSsum,
                  totalCompensation: formattedTotalCompensation,
                  compulsoryMatrix: [
                      { id: 1, amounts: [A1, A2, A3].map(val => this.roundToTwoDecimal(val)), sum: formattedAsum },
                      { id: 2, amounts: [B1, B2, B3].map(val => this.roundToTwoDecimal(val)), sum: formattedBsum },
                      { id: 3, amounts: [C1, C2, C3].map(val => this.roundToTwoDecimal(val)), sum: formattedCsum }
                  ]
              });



计算器在运行时候, <text class="result-cell">{{(J[item-1]) + (X[item-1]) +(S[item-1])}}</text>部分条件下存在浮点小数,没有办法解决.网
上查了一下称“(J[item-1]) + (X[item-1]) +(S[item-1])”直接计算时,不受二级控制,因为分支逻辑太多,之前试了很多办法均宣告失败,请求大神
能否有最简单的方式去解决这个问题,感谢!

回答关注问题邀请回答
收藏

2 个回答

  • yoko
    yoko
    7小时前

    你问问AI 会不会 好一些

    7小时前
    有用
    回复
  • Mr.Zhao
    Mr.Zhao
    17小时前

    这写的什么demo啊?

    17小时前
    有用
    回复
登录 后发表内容