Re: Formatting a set of NSDecimalNumber values
Re: Formatting a set of NSDecimalNumber values
- Subject: Re: Formatting a set of NSDecimalNumber values
- From: Jens Alfke <email@hidden>
- Date: Fri, 10 Aug 2012 09:29:28 -0700
On Aug 10, 2012, at 8:58 AM, Sixten Otto <email@hidden> wrote:
> In order to make the columns line up, and to know how much space I'll
> need, what I'd like to do is to figure out, for each set of numbers,
> the width of the format I'll need. In other words, the maximum number
> of integer places, and the maximum number of fractional places.
Just to make things even more complicated: you're assuming digits are monospaced. In many fonts they are not — it generally looks better if a 1 is narrower than a 0, for instance. The "." is also pretty much guaranteed to be a lot narrower than a digit, and the "e" in scientific notation will be a different width too.
There's also the matter that number formatting is localizable. Many countries use "," for a decimal point.
> One workaround might be to convert each number to a string in the
> POSIX locale, and then measure its length and the position of the
> decimal point.
That doesn't seem like a workaround, it seems like the correct solution (although why not use the user's real locale?). Fundamentally you are trying to display and align character strings, so you have to treat the numbers as strings when computing their alignment. For best results use AppKit (or UIKit?) methods to compute the pixel width of each string in the font you're using.
—Jens
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden