Skip to main content
JR Boulay
Community Expert
Community Expert
Nov 19, 2016 6:43 PM UTC
Answered

util.printf()

Hi.

I know how to use util.printf() to get a number formated as: 1234.56

n = util.printf("%.2f", n);

What I need is a number formated as: 1234,56

With a comma instead of a dot.

How can I do that?

Where can I find some infos since I cannot find nothing in the JS Reference?

Thank you.

This topic is closed to new replies. Start a new post to keep the conversation going.
gkaiserilCorrect answer
Inspiring
Nov 19, 2016 8:23 PM UTC

I would add the "nDec" parameter with a value of ",2".

var nValue = 123.56;

var nDec = ",2";

var cFlags = "";

var nWidth = "";

var nPrecision = ".2";

var cConvChar = "f";

var cFormat = "%" + nDec + cFlags + nWidth + nPrecision + cConvChar;

console.println("cFormat: " + cFormat);

var cValue = util.printf(cFormat, nValue);

console.println("nDec " + nDec + " — Period separated, comma decimal point : " + cValue);

It is explained under the "util" object and the "printf" method.

3 replies

JR Boulay
Community Expert
JR BoulayCommunity ExpertAuthor
Community Expert
Nov 20, 2016 11:10 AM UTC

the French format is closest if the "nDec" parameter have a value of ",3".

It is close but not satisfactory since French format requires blank spaces as separators:

10 234 567 890,55

Since French separators are not available directly as a format I use a "dot separators" format, and I replace dots by blank spaces:

n = util.printf("%,2.2f",n).toString().replace(/\./gim," ");

And the goal is reached!

Thank you once again.

En français :

Le format de nombre français n'étant pas disponible directement dans les formats, la ruse dans la formule ci-dessus consiste à utiliser un format qui place des points comme séparateurs de milliers, et à remplacer à la volée les points par des espaces.

Translate
Acrobate du PDF, InDesigner et Photoshopographe
JR Boulay
Community Expert
JR BoulayCommunity ExpertAuthor
Community Expert
Nov 19, 2016 10:32 PM UTC

Great great great, it works fine.

In fact, the French format is closest if the "nDec" parameter have a value of ",3".

Thank you very much, I learned something today.

Translate
Acrobate du PDF, InDesigner et Photoshopographe
gkaiserilCorrect answer
Inspiring
Nov 19, 2016 8:23 PM UTC

I would add the "nDec" parameter with a value of ",2".

var nValue = 123.56;

var nDec = ",2";

var cFlags = "";

var nWidth = "";

var nPrecision = ".2";

var cConvChar = "f";

var cFormat = "%" + nDec + cFlags + nWidth + nPrecision + cConvChar;

console.println("cFormat: " + cFormat);

var cValue = util.printf(cFormat, nValue);

console.println("nDec " + nDec + " — Period separated, comma decimal point : " + cValue);

It is explained under the "util" object and the "printf" method.

Translate