簡體   English   中英

修復不起作用

[英]toFixed not working

我有一個節省的價格,正在使用Math.ceil方法將輸出舍入到最接近的£,但是當我實現toFixed給我25.00英鎊時,我得到的只是25英鎊。

我的代碼是這樣的:

jQuery(function($) {
    var price = $('.product-info-short meta[itemprop="price"]').attr("content");
    var discount = price/10;
    var earlyBirdPrice = price-discount;
    var div = document.querySelector('.earlyBirdPrice');


//method for early bird price//
div.innerHTML += '<div class="earlyBirdPrice">Price: £' + 
Number(earlyBirdPrice.toFixed(2)).toLocaleString('en') + '</div>';

//method for discount price//
div.innerHTML += '<div class="discount"> Saving you £' + 
Number(Math.ceil(discount.toFixed(2))).toLocaleString('en') + 
'</div>';

});

js在這里撥弄: https : //jsfiddle.net/y18h9s7f/

我究竟做錯了什么?

您正在做的錯誤是,在使用toFixed轉換為字符串之后,您將轉換回一個數字,然后對該數字調用toLocaleString('en')

可以使用toFixed獲得非語言環境特定的字符串與給定的小數位數, toLocaleString('en')來獲取特定區域設置的字符串的地方首選號碼,但不能同時使用。

如果目標環境支持Intl.NumberFormatspec | MDN ),則可能要看一下它(可以對其進行功能測試,然后再使用其他方法)。 例:

 const formatter = typeof Intl === "object" && Intl.NumberFormat ? new Intl.NumberFormat('en', { // Yay, we have Intl.NumberFormat minimumFractionDigits: 2, maximumFractionDigits: 2 }) : {format: n => n.toFixed(2) }; // Boo, fall back. (Maybe something more intricate here.) console.log(formatter.format(42.546789)); 

輸出42.55 ,因為我使用的是en語言環境。 如果我將de用作德語,則顯示42,55

 const formatter = typeof Intl === "object" && Intl.NumberFormat ? new Intl.NumberFormat('de', { // Yay, we have Intl.NumberFormat minimumFractionDigits: 2, maximumFractionDigits: 2 }) : {format: n => n.toFixed(2) }; // Boo, fall back. (Maybe something more intricate here.) console.log(formatter.format(42.546789)); 

這使用瀏覽器的默認語言環境(例如,特定於用戶):

 const formatter = typeof Intl === "object" && Intl.NumberFormat ? new Intl.NumberFormat(undefined, { // Yay, we have Intl.NumberFormat minimumFractionDigits: 2, maximumFractionDigits: 2 }) : {format: n => n.toFixed(2) }; // Boo, fall back. (Maybe something more intricate here.) console.log(formatter.format(42.546789)); 

嘗試這個:-

div.innerHTML += '<div class="discount"> Saving you £' + Number(Math.ceil(discount)).toFixed(2) + '</div>';

演示:-https: //jsfiddle.net/f18o1v91/

您在toFixed()之上應用Math.ceil() / Number() ,因此結果是一個Number,而不是浮點字符串。

你需要

  • 刪除Number構造函數包裝
  • 直接將Math.ceil應用於discount

做了

Math.ceil(discount).toFixed(2)

我只得到25英鎊。

基本上,如果小數點后只有0,則Number constructor將刪除小數點。

 console.log( Number( "25.00" ) ); var discount = 25.00; console.log(Math.ceil(discount).toFixed(2)) 

您顯然需要刪除Number包裝:

來自:

Number(Math.ceil(discount.toFixed(2))).toLocaleString('en')

至 :

Math.ceil(discount).toFixed(2).toLocaleString('en')

嘿@Robbie,我找到了您的問題並解決了。 您錯過了應用parseFloat()的操作。 我在jsfddl中更改您的代碼。 請看看我的解決方案。

//method for discount price//
div.innerHTML += '<div class="discount"> Saving you £' + parseFloat(Number(Math.ceil(discount.toFixed(2))).toLocaleString('en')).toFixed(2) + '</div>';

這是JSfiddle鏈接: JSFiddle https://jsfiddle.net/sajibremix/uj3qkd6r/我想您找到了解決方案。 快樂的編碼:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM