GreenTriangle
GreenTriangle

Reputation: 2460

Trailing zeroes in a format string?

I have figures representing monetary amounts coming in -- 45.10, 24.35, 17.99, and so on.

I want to split these into the dollar and cent portions, do something to the dollars, and then output a string of dollars + '.' + cents.

PROBLEM: The figure .10 obviously becomes 1, and I don't want to output $84.1. I want $84.10. So the format string should specify "two-digit integer, with a trailing zero if there's only one digit."

Any search I do just turns up results for leading zeroes. Is this possible?

Upvotes: 1

Views: 669

Answers (1)

iced
iced

Reputation: 1572

You need sprintf:

sprintf("%d.%02d", dollars, cents) # must be numbers

Upvotes: 3

Related Questions