快速格式化高度

时间:2019-02-14 15:22:46

标签: swift foundation measurement

我想以一种易于理解的方式设置高度,我想检查一下我是否在重新发明轮子。

所以说我要输出5'1“ 1/8

Foundation框架是开箱即用的还是我需要自己动手做?我并没有要求任何人进行滚动,只是我没有错过这样做的预期方式。

import UIKit

let feet = Measurement<UnitLength>(value: 5, unit: .feet)
print(feet)

let inches = Measurement<UnitLength>(value: 1.125, unit: .inches)
print(inches)

let height = (feet + inches).converted(to: .feet)
print(height)

// I want to output 5' 1" 1/16

let formatter = MeasurementFormatter()
formatter.locale = Locale(identifier: "en_US")

formatter.unitOptions = .naturalScale

formatter.unitStyle = .long
print(formatter.string(from: height))
formatter.unitStyle = .medium
print(formatter.string(from: height))
formatter.unitStyle = .short
print(formatter.string(from: height))

输出

5.0 ft
1.125 in
5.09375 ft
61.125 inches
61.125 in
61.125″

0 个答案:

没有答案
相关问题