-1

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));
});
Lionel Rowe
  • 5,164
  • 1
  • 14
  • 27
H_Hmd
  • 25
  • 8
  • Multiply the value by -1 – Ben Brookes Aug 27 '20 at 14:49
  • Thank you @BenBrookes for replying. you mean $(['#cart-subtotal-discount' .value*(-1)]) ?? – H_Hmd Aug 27 '20 at 14:54
  • `$(['#cart-subtotal-discount' .value])` doesn't really make sense. If you know that the number is positive, you can just do `$('#cart-subtotal-discount .value').prepend('-')`. Or even better, a CSS rule `#cart-subtotal-discount .value::before { content: "-"; }` – Guy Incognito Aug 27 '20 at 14:58

1 Answers1

2

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>
Lionel Rowe
  • 5,164
  • 1
  • 14
  • 27