Skip to main content
JR Boulay
Community Expert
Community Expert
November 19, 2016
Answered

util.printf()

  • November 19, 2016
  • 3 replies
  • 3851 views

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 has been closed for replies.
Correct answer gkaiseril

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
November 20, 2016

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.

Acrobate du PDF, InDesigner et Photoshopographe
JR Boulay
Community Expert
JR BoulayCommunity ExpertAuthor
Community Expert
November 19, 2016

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.

Acrobate du PDF, InDesigner et Photoshopographe
gkaiserilCorrect answer
Inspiring
November 19, 2016

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.