Passaggio all'ora legale 31 marzo 2024 02:00 03:00 sposta avanti l'orologio di 1 ora (si dorme 1 ora in meno)
Questa mini libreria in JavaScript permette di gestire la formattazione e il parsing dei numeri secondo le convenzioni italiane.

Si compone di 3 metodi:
  • sgart.isValidNumberIT(strNumero): ritorna true se la stringa numerica passata è formalmente corretta, altrimenti false
  • sgart.parseNumberIT(strNumero): passando una stringa che rappresenta un numero, lo ritorna in formato numerico
  • sgart.formatNumberIT(n, numDecimali): passando un numero e il numero di decimali, ritorna una stringa numerica formattata
JavaScript
/* 
 * http://www.sgart.it
 * funzione per formattare e parsare i numeri in formato italiano
 */
var sgart = sgart || {};

/*
 * controlla se una stringa che contiene un numero (strNumero) 
 * è nel formato valido secondo le convenzioni italiane
 */
sgart.isValidNumberIT = function (strNumero) {
	if (typeof strNumero !== "string" || strNumero == null) return false;
	var s = strNumero.replace(/\./g, "").replace(",", ".");
	if(/^[\+\-]?[0-9]+(\.[0-9]+)?$/.test(s) === false) return false;
	if(parseFloat(s) === NaN) return false;
	return true;
};

/*
 * passando una stringa che rappresenta un numero (strNumero)
 * ad esempio "-12.456,78901", la converte in numero
 */
sgart.parseNumberIT = function (strNumero) {
	if (typeof strNumero !== "string") throw "Not a string";
	if (strNumero == null) return 0;
	var s = strNumero.replace(/\./g, "").replace(",", ".");
	if(/^[\+\-]?[0-9]+(\.[0-9]+)?$/.test(s) === false) throw "Not a valid number";
	return parseFloat(s);
};

/*
 * passando un numero (n), ritorna una stringa formattata
 * secondo la convenzione italiana, 
 * con i separatori delle migliaia e il numero di decimali richiesto (numDecimali)
 * ad esempio; 12456.78901 => "12.456,78901"
 */
sgart.formatNumberIT = function (n, numDecimali) {
  if (typeof n === "undefined" || n == null) return "-";  // nessun numero passato
  if(typeof n !== "number") throw "Not a number";
  if(typeof numDecimal === undefined) numDecimal = 0;
  var sepMigliaia = ".";
  var sepDecimali = ",";
  var strNumero = n.toString();
  var n1 = strNumero;
  var d1 = "";

  var i = strNumero.indexOf(".");
  if(i !== -1){
	n1 = strNumero.substring(0, i);
	if(numDecimali > 0) {
		d1 = sepDecimali + (strNumero.substring(i + 1) + "000000000000000").substring(0, numDecimali);
	}
  }
  // migliaia
  var m = n1.length % 3;
  if (m === 0) m = 3;
  var s = "";
  i = 0;
  while (i < n1.length) {
    if (m === 0) {
      s += sepMigliaia;
      m = 3;
    }
    s += n1[i];
    i++;
    m--;
  }
  return s + d1;
};
questi sono esempi di utilizzo:
JavaScript
sgart.isValidNumberIT("113s323,51231231")   // false (c'è una lettera tra i numeri)
sgart.isValidNumberIT("113.323,51231231")   // true

sgart.parseNumberIT("113s323,51231231")     // Not a valid number
sgart.parseNumberIT("113.323,51231231")     // 113323.51231231 (numero)

sgart.formatNumberIT("-23.113.323,51231231", 3)    // Not a number (non posso passare una stringa)
sgart.formatNumberIT(-23113323.51231231, 3)        // "-23.113.323,512"
Potrebbe interessarti anche: