使用 NSMeasurementFormatterUnitOptionsNaturalScale 选项强制 DNS MeasurementFormatter 获得超过 1 的值

Force NSMeasurementFormatter to get value over 1 with NSMeasurementFormatterUnitOptionsNaturalScale optiion

如果我们将值 100g 放入 NSUnit 并使用 NSMeasurementFormatterUnitOptionsNaturalScale 获取翻译后的字符串,则英制系统的结果为 3.52 oz。 问题出在公制上,结果是 0.1 公斤,这是一个很好的价值,但我需要保持 100 克。这是一个食谱,当价值低于 1 公斤时,更常见的是保持 "g"。

用米测试不存在这个问题,100m 是公制的 100m 可能是因为米是像 kg(而不是 g)这样的基值。

有没有办法强制它的值大于 1 而不是 0,xx?

NSUnitMass *g = [NSUnitMass grams];
NSMeasurement *measurementGramm = [[NSMeasurement alloc] initWithDoubleValue:100 unit:g];
NSMeasurementFormatter *formatter = [[NSMeasurementFormatter alloc] init];
[formatter setUnitOptions:NSMeasurementFormatterUnitOptionsNaturalScale];
NSLog(@"NSMeasurementFormatterUnitOptionsNaturalScale ---------- %@", [formatter stringFromUnit:g]);   

为方便起见,我假设储值是按公斤计算的。

NSMeasurement *measurement = [[NSMeasurement alloc] initWithDoubleValue:0.1 unit:NSUnitMass.kilograms];
NSLog(@"Measurement value = %@", measurement);

NSLog 结果

Measurement value = <NSMeasurement: 0x28028ada0> value: 0.100000 unit: kg

这里我们可以看到我们的值是0.1,单位是kg

It's for a recipe and and it's more common to keep "g" when the value is below 1 kg.

如果 value 低于 1

,我们可以转换 unit
NSMeasurementFormatter *formatter = [[NSMeasurementFormatter alloc] init];

NSLog(@"Locale (KG) measurement value = %@", [formatter stringFromMeasurement:measurement]);

if (measurement.doubleValue < 1.0) {
    NSLog(@"Locale (G) measurement value = %@", [formatter stringFromMeasurement:[measurement measurementByConvertingToUnit:NSUnitMass.grams]]);
} else {
    NSLog(@"Locale (KG) measurement value = %@", [formatter stringFromMeasurement:measurement]);
}

NSLog 结果

Locale (KG) measurement value = 0,1 kg
Locale (G) measurement value = 0,1 kg

结果还在kg,这是unitOptions

造成的

By default, if unitOptions is set to the empty set, the formatter will do the following:

  • kilocalories may be formatted as "C" instead of "kcal" depending on the locale.
  • kilometersPerHour may be formatted as "miles per hour" for US and UK locales but "kilometers per hour" for other locales.

我们只需要改变它

formatter.unitOptions = NSMeasurementFormatterUnitOptionsProvidedUnit;

if (measurement.doubleValue < 1.0) {
    NSLog(@"Locale (G) measurement value = %@", [formatter stringFromMeasurement:[measurement measurementByConvertingToUnit:NSUnitMass.grams]]);
} else {
    NSLog(@"Locale (KG) measurement value = %@", [formatter stringFromMeasurement:measurement]);
}

NSLog 结果

Locale (G) measurement value = 100 g

完整代码

NSMeasurement *measurement = [[NSMeasurement alloc] initWithDoubleValue:0.1 unit:NSUnitMass.kilograms];
NSLog(@"Measurement value = %@", measurement);

NSMeasurementFormatter *formatter = [[NSMeasurementFormatter alloc] init];

NSLog(@"Locale (KG) measurement value = %@", [formatter stringFromMeasurement:measurement]);

if (measurement.doubleValue < 1.0) {
    NSLog(@"Locale (G) measurement value = %@", [formatter stringFromMeasurement:[measurement measurementByConvertingToUnit:NSUnitMass.grams]]);
} else {
    NSLog(@"Locale (KG) measurement value = %@", [formatter stringFromMeasurement:measurement]);
}

formatter.unitOptions = NSMeasurementFormatterUnitOptionsProvidedUnit;

if (measurement.doubleValue < 1.0) {
    NSLog(@"Locale (G) measurement value = %@", [formatter stringFromMeasurement:[measurement measurementByConvertingToUnit:NSUnitMass.grams]]);
} else {
    NSLog(@"Locale (KG) measurement value = %@", [formatter stringFromMeasurement:measurement]);
}