Advertisement
dwhitzzz

Formatta/Deformatta number js

Apr 26th, 2017
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Formatta l'importo di testo passato in input.
  3.  * Ritorna l'importo numerico formattato.
  4.  *
  5.  * @param {String} importo in stringa
  6.  * @param {int} nDec - numero decimali, se non passato utilizza il n. defualt configurato lato server
  7.  * @function
  8.  */
  9. function formattaTestoImporto(text, nDec) {
  10.   if (nDec != null) {
  11.     var formato = creaFormatoByDecimali(nDec);
  12.     return jQuery.formatNumber(text, { format: formato });
  13.   } else {
  14.     return jQuery.formatNumber(text);
  15.   }
  16. }
  17.  
  18. //es. formattaTestoImporto(55.5) -> return "55,50"
  19.  
  20. ---------------------------
  21.  
  22.  
  23. /**
  24.  * Deformatta il testo passato in input.
  25.  * Ritorna l'importo numerico (Number) deformattato (9999.00).
  26.  * In caso di errore ritorna null
  27.  *
  28.  * @param {String} text - importo in stringa
  29.  * @function
  30.  */
  31. function deformattaTestoImporto(text) {
  32.   var number = null;
  33.   try {
  34.     number = jQuery.parseNumber(text);
  35.   } catch (error) {
  36.     return null;
  37.   }
  38.   return number;
  39. }
  40.  
  41. // es . deformattaTestoImporto('99.999,00') -> return 99999;
  42. //      deformattaTestoImporto('555,85') ->  return 555.85
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement