r/javascript • • Jan 28 '12

how come javascript doesn't have printf?

0 Upvotes

13 comments sorted by

View all comments

Show parent comments

1

u/x-skeww Jan 28 '12

printf is short for "print formatted" (the 's' in sprintf stands for string, you "print" into a string). It does a bit more than blind concatenation. E.g. you can print a number as hex value, you can specify the amount of decimal places, you can print leading zeros, and things like that.

1

u/[deleted] Jan 28 '12

yeah I know, but I mean because of the loose typing, you can concat decimals with strings, eg:

var formatted = "Your total is $"+(82.324324).toFixed(2);

and it won't complain, unlike in C where AFAIK concatting a string with a decimal was a PITA and so printf was a nice way around that. In JS - just concat all the variables of various types and you'll be fine. OK so you'll have to format them yourself but that's not hard.

edit: ok, toFixed returns a string, but you get what I mean

var formatted = "Your total is $"+(Math.round(82.324324*100)/100);

works.

2

u/x-skeww Jan 28 '12

var formatted = sprintf('Your total is $%.2f', 82.324324);

It's a lot easier to read. Especially if there is more than one value and if there are spaces next to your values.

E.g.:

var formatted = 'Hey, ' + firstName + ' ' + lastName + ' your total is $' + (total).toFixed(2) + '.';
var formatted = sprintf('Hey, %s %s your total is $%.2f.', firstName, lastName, total);

The first one is kinda error prone to type. Often Komodo will give me a squiggly red underline and I have to reread it a few times until I finally find the missing '+'.

A quick and dirty Handlebars example (untested):

Handlebars.registerHelper('dollars', function(money) {
	return '$' + (+money).toFixed(2);
});
[...]
var template = Handlebars.compile('Hey, {{firstName}} {{lastName}} your total is {{dollars total}}';
[...]
var formatted = template({
	firstName: 'Peter',
	lastName: 'Parker',
	total: 82.324324
});

That dollars helper function can be reused application wide. It could be also something more generic, which handles all kinds of currencies and also different kinds of number formatting.

If I understand ECMA6 quasi literals correctly, something like that could work at some point in the future (untested, naturally):

var template = function (ctx) {
	return `Hey, ${ctx.firstName} ${ctx.lastName} your total is ${(total).toFixed(2)}.`; //yes, backticks
}
var formatted = template({
	firstName: 'Peter',
	lastName: 'Parker',
	total: 82.324324
});

2

u/[deleted] Jan 28 '12
var formatted = 'Hey, ' + firstName + ' ' + lastName + ' your total is $' + (total).toFixed(2) + '.';
var formatted = sprintf('Hey, %s %s your total is $%.2f.', firstName, lastName, total);

I really don't like the latter. what happens when things get more complicated and you have 5 or 6 variables? You then have to count the %s's and the vars. I find it much easier to write, edit, read and debug the former. Yes honestly.

Just skim over the two lines quickly.

The first says "Hey firstname lastname your total is $total tofixed 2".

The second says "Hey s s your total is $ 2f firstname lastname total"

I honestly can't see how the latter is preferable. It offers a convenient method for formatting, yes, but I find it much less readable.

The former is more prone to syntax errors - which happen once, and then are fixed. You read code more than you write it. Therefore optimise for performance, then readability, then writeability.