Question about formatting of Measurement<...> items

I was experimenting in a playground with the specifying the kind of measurement with MeasurementUnit and then formatting it with the new foundation formatters, specifically MeasurementFormatter.

The particular example I was using is a UnitDuration, exploring the various options and outputs.

let times = [1,10,100,1000,10103,2354,83674,182549].map {
    Measurement<UnitDuration>(value: $0, unit: .microseconds)
}
print("unitStyle: .medium, unitOptions: .providedUnit")
f.unitOptions = .providedUnit
f.unitStyle = .medium
for t in times {
    print(f.string(from: t))
}

What I gathered from this exploration is that the unit you use when you create a Measurement<UnitDuration> is stashed within it - in this case I created them with microseconds.

What I'd like to do have a uniform scale for the times I'm printing that more aligned with the size based on the values. For example, if all my examples are recorded in microseconds, but more usefully represent milliseconds - is there a way to deal with this just in the formatting itself?

I tried updating the unit representation to transform the MeasurementUnit<Duration> instances, but the unit property is a constant: Cannot assign to property: 'unit' is a 'let' constant. I did find the convert(to:) that would let me transform to another specific unit.

Is there, by chance, any way to pass in the unit to use directly to a MeasurementFormatter instance, or do I need to back out and transform the various instances to get to a specific unit?

Likewise, are there any built-in methods to MeasurementUnit that align the SI units chosen to the scale of the data being used as a value? Or is that something I'd need to create a heuristic/algorithm to and do and apply it myself when I wanted that result?

The full playground content in case anyone else comes exploring:

import Foundation //var time1 = Measurement<UnitDuration>(value: 1.23, unit: .seconds) var time2 = Measurement<UnitDuration>(value: 6.54, unit: .milliseconds) //var time3 = Measurement<UnitDuration>(value: 9.01, unit: .microseconds) time2.converted(to: .picoseconds) let times = [1,10,100,1000,10103,2354,83674,182549].map { Measurement<UnitDuration>(value: $0, unit: .microseconds) } //print("\(time1), \(time2), \(time3)") //print(time1.formatted()) //print(time2.formatted()) //print(time3.formatted()) //for t in times { //    print(t.formatted()) //} // Static method 'list(type:width:)' requires the types 'Measurement<UnitDuration>' and 'String' be equivalent // print(times.formatted(.list(type: .and, width: .standard))) let f = MeasurementFormatter() print("unitStyle: .short, unitOptions: .naturalScale") f.unitOptions = .naturalScale f.unitStyle = .short for t in times {     print(f.string(from: t)) } print("unitStyle: .medium, unitOptions: .naturalScale") f.unitOptions = .naturalScale f.unitStyle = .medium for t in times {     print(f.string(from: t)) } print("unitStyle: .long, unitOptions: .naturalScale") f.unitOptions = .naturalScale f.unitStyle = .long for t in times {     print(f.string(from: t)) } print("unitStyle: .short, unitOptions: .providedUnit") f.unitOptions = .providedUnit f.unitStyle = .short for t in times {     print(f.string(from: t)) } print("unitStyle: .medium, unitOptions: .providedUnit") f.unitOptions = .providedUnit f.unitStyle = .medium for t in times {     print(f.string(from: t)) } print("unitStyle: .long, unitOptions: .providedUnit") f.unitOptions = .providedUnit f.unitStyle = .long for t in times {     print(f.string(from: t)) }

Please re-add the unreadable code above in the Your Answer box using the code block feature.

import Foundation
let times = [1,10,100,1000,10103,2354,83674,182549].map {
    Measurement<UnitDuration>(value: $0, unit: .microseconds)
}
// convert times from microseconds to milliseconds
let milliseconds = times.map { $0.converted(to: .milliseconds)}
milliseconds.forEach { print($0.value) }
// output
0.001
0.009999999999999998
0.09999999999999999
1.0
10.103
2.3539999999999996
83.67399999999999
182.54899999999998

Or to mutate a measurement inplace

var micro = Measurement<UnitDuration>(value: 1000000, unit: .microseconds)
micro.convert(to: .milliseconds)
print(micro.value)
// output
1000.00

Thanks @MobileTen - that's what I'd found I could do - I was just hoping there might be a path to leave the measurement itself alone and include a unit value to the formatter itself, but that apparently isn't a thing.

Question about formatting of Measurement&lt;...&gt; items
 
 
Q