Formatting And Converting Weights

how-to
foundation
asobjc

(Mark Alldritt) #1

NSMassFormatter can help format weights, and even perform conversions from a default unit to localized equivalents. The default unit is kilograms.

Run this script as an example:

use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set mFormatter to current application's NSMassFormatter's new()
mFormatter's setUnitStyle:(current application's NSFormattingUnitStyleLong)
set theWeight to (mFormatter's stringFromKilograms:85) as text

If your system is set to use metric weights and measures, the result will be “85 kilograms”. On a US system it will be “187.393 pounds”.

You can also choose from a selection of units, such as this example:

set mFormatter to current application's NSMassFormatter's new()
mFormatter's setUnitStyle:(current application's NSFormattingUnitStyleLong)
set theWeight to (mFormatter's stringFromValue:150 unit:(current application's NSMassFormatterUnitPound)) as text
--> "150 pounds"