2

I display some prices and numbers from server with PHP, using functions to format in french. Then I want to use Jquery to do some calculations client-side. I use .toLocaleString('fr-FR') to format the results to show. It works in console but not in DOM.

Here is the code:

resultat_partiel = 1;

resultat_partiel *= parseFloat($(this).text().replace(/ /g, ''), 10);

console.log(resultat_partiel, resultat_partiel.toLocaleString('fr-FR'));

$('div.resultat_partiel').text(parseFloat(resultat_partiel).toLocaleString('fr-FR'));   

var resultat = 0;
var resultats_partiels = $(this).find('.resultat_partiel');
resultats_partiels.each(function(){
console.log($(this).text(), parseFloat($(this).text(), 10));
resultat += parseFloat($(this).text());
    });
$(tbody).find($('td.resultat')).text(resultat);

Here is what I get: enter image description here

ParseInt or parseFloat, none solves the issue. What am I doing wrong please? Thanks

Cutis
  • 949
  • 1
  • 13
  • 32
  • Try this `$(this).text() = $(this).text().replace(/ /g, '');` after code `resultat_partiel = 1;` – Niklesh Raut May 02 '19 at 09:15
  • try `var a = parseFloat(resultat_partiel); var b = a.toLocaleString('fr-FR') $('div.resultat_partiel').text(b);` – fmsthird May 02 '19 at 09:19
  • I have already try it @fmsthird, this does not change anything. did you try it? – Cutis May 02 '19 at 09:35
  • Why parseFloat(resultat_partiel).toLocaleString('fr-FR') ? resultat_partiel is already float. In console.log , you have printed resultat_partiel.toLocaleString('fr-FR') but in $('div.resultat_partiel').text , you are converting the resultat_partiel into float before applying the toLocaleString. – sudip May 03 '19 at 10:07
  • @sudip I try a lot of things to see if any difference. I just copy the code and paste because, no way to reach what I expect. but this is not the issue. – Cutis May 03 '19 at 10:11

2 Answers2

0

I finally use replace(/\s/g, '') instead of replace(/ /g, '') But showing number in french format in DOM is always not working.

Cutis
  • 949
  • 1
  • 13
  • 32
0

I don't know if it's a spelling error but I tried adding a space to your replace function and it worked fine:

value.replace(/\s/g, ' ')

L. G.
  • 35
  • 10