Existe-t-il un moyen simple de formater des nombres en JavaScript, similaire aux méthodes de formatage disponibles en C# (ou VB.NET) via ToString("format_provider" ;)
ou String.Format()
?
Généralement
En jQuery
[1] : http://www.mredkj.com/javascript/numberFormat.html [2] : http://geekswithblogs.net/rajeshatdotnet/archive/2008/04/05/121058.aspx [3] : http://www.decorplanit.com/plugin/ [4] : http://www.asual.com/jquery/format/ [5] : http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html [6] : http://docs.oracle.com/javase/7/docs/api/java/text/NumberFormat.html [7] : http://code.google.com/p/jquery-numberformatter/
Vous devriez probablement jeter un coup d'œil au plugin JQuery NUMBERFORMATTER :
[Jquery Number Formatter][1]
Et cette question :
[Une façon plus simple de formater les nombres en Javascript] [2]
[1] : https://code.google.com/p/jquery-numberformatter/ [2] : https://stackoverflow.com/questions/726144/javascript-easier-way-to-format-numbers/
Tout d'abord, la conversion d'un nombre entier en chaîne de caractères en JS est très simple :
// Start off with a number
var number = 42;
// Convert into a string by appending an empty (or whatever you like as a string) to it
var string = 42+'';
// No extra conversion is needed, even though you could actually do
var alsoString = number.toString();
Si vous avez un nombre sous forme de chaîne et que vous voulez le transformer en entier, vous devez utiliser la fonction parseInt(string)
pour les entiers et parseFloat(string)
pour les flottants. Ces deux fonctions renvoient alors le nombre entier ou le nombre flottant souhaité. Exemple :
// Start off with a float as a string
var stringFloat = '3.14';
// And an int as a string
var stringInt = '42';
// typeof stringInt would give you 'string'
// Get the real float from the string
var realFloat = parseFloat(someFloat);
// Same for the int
var realInt = parseInt(stringInt);
// but typeof realInt will now give you 'number'
Ce que vous essayez exactement d'ajouter, etc., n'est pas clair pour moi dans votre question.