将正数转换为负数
transform a positive number to a negative number
我想使用减号将数字显示为负数:5000 --> - 5000
<div class="cart-summary-line" id="cart-subtotal-discount">
<span class="label">
Remise
</span>
<span class="value">5,000 TND</span>
</div>
这是我在自定义JS中写的文章,但数字仍然显示为正数
$(document).ready(function() {
return "-" + ($(['#cart-subtotal-discount'.value]).html(5.000));
});
您的 JavaScript 和 jQuery 语法有很多问题。我建议 re-reading 通过 jQuery documentation and examples。
$(document).ready(function() {
return "-"+($(['#cart-subtotal-discount'.value]).html(5.000));
// ^ we don't need the return value
// ^ concatenating a "-" here won't set anything
// ^ don't need the extra parentheses here
// ^ why square brackets? this isn't an array
// ^ this selector contains a lot more text than just the number
// ^ do we want `value` or `html`?
// ^ are we getting, setting, or both?
// ^ `5.000` means `5` in JS, not `5000`
});
这是您可以实现目标的一种方法:
const positiveNumber = 5000
const negativeNumber = 0 - positiveNumber
const locale = 'ar-TN' // Tunisian locale, assumed from "TND" currency
// Using `toLocaleString` with the ar-TN locale will give you
// the "5.000" formatting you want, but you still need to
// write the number as `5000` in JavaScript.
const formattedNumber = negativeNumber.toLocaleString(locale)
$(document).ready(function() {
$('#amount').html(formattedNumber)
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p>Remise <span id="amount"></span> TND</p>
我想使用减号将数字显示为负数:5000 --> - 5000
<div class="cart-summary-line" id="cart-subtotal-discount">
<span class="label">
Remise
</span>
<span class="value">5,000 TND</span>
</div>
这是我在自定义JS中写的文章,但数字仍然显示为正数
$(document).ready(function() {
return "-" + ($(['#cart-subtotal-discount'.value]).html(5.000));
});
您的 JavaScript 和 jQuery 语法有很多问题。我建议 re-reading 通过 jQuery documentation and examples。
$(document).ready(function() {
return "-"+($(['#cart-subtotal-discount'.value]).html(5.000));
// ^ we don't need the return value
// ^ concatenating a "-" here won't set anything
// ^ don't need the extra parentheses here
// ^ why square brackets? this isn't an array
// ^ this selector contains a lot more text than just the number
// ^ do we want `value` or `html`?
// ^ are we getting, setting, or both?
// ^ `5.000` means `5` in JS, not `5000`
});
这是您可以实现目标的一种方法:
const positiveNumber = 5000
const negativeNumber = 0 - positiveNumber
const locale = 'ar-TN' // Tunisian locale, assumed from "TND" currency
// Using `toLocaleString` with the ar-TN locale will give you
// the "5.000" formatting you want, but you still need to
// write the number as `5000` in JavaScript.
const formattedNumber = negativeNumber.toLocaleString(locale)
$(document).ready(function() {
$('#amount').html(formattedNumber)
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p>Remise <span id="amount"></span> TND</p>