简体   繁体   中英

transform a positive number to a negative number

I want to display a number as negative using a minus sign: 5000 --> - 5000

<div class="cart-summary-line" id="cart-subtotal-discount">
  <span class="label">
    Remise
  </span>
  <span class="value">5,000&nbsp;TND</span>
</div>

This is my essay in custom JS, but the number still displays as positive.

$(document).ready(function() {
  return "-" + ($(['#cart-subtotal-discount'.value]).html(5.000));
});

You have quite a few problems with your JavaScript and jQuery syntax. I'd suggest re-reading through the 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`

});

Here's one way you might achieve what you're trying to do:

 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>&nbsp;TND</p>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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