Macos DateComponents能否格式化分数秒?
例如,我想像Safari的时间线一样,将值1.5打印为“1.5秒”,我正在尝试使用DateComponents格式 不幸的是,它的Macos DateComponents能否格式化分数秒?,macos,foundation,nsdatecomponents,nsdatecomponentsformatter,Macos,Foundation,Nsdatecomponents,Nsdatecomponentsformatter,例如,我想像Safari的时间线一样,将值1.5打印为“1.5秒”,我正在尝试使用DateComponents格式 不幸的是,它的.allowedUnits只会下降到.second(即使枚举有.nanosecond)。我尝试设置.allowsFractilunits=true,但仍然得到“0秒” 有没有办法从DateComponents格式中获取小数秒数?对于位置单位样式,DateComponents格式可以与NumberFormatter一起使用,以获取区域设置感知字符串 func格式(秒:时
.allowedUnits
只会下降到.second
(即使枚举有.nanosecond
)。我尝试设置.allowsFractilunits=true
,但仍然得到“0秒”
有没有办法从DateComponents格式中获取小数秒数?对于位置单位样式,
DateComponents格式可以与NumberFormatter
一起使用,以获取区域设置感知字符串
func格式(秒:时间间隔)->字符串{
让components=DateComponentsFormatter()
components.allowedUnits=[分钟,.秒]
components.AllowsFractalUnits=true//未按预期工作
components.unitsStyle=.positional
components.zeroFormattingBehavior=.pad
设分数=NumberFormatter()
分数.maximumIntegerDigits=0
分数.minimumFractionDigits=0
分数.maximumFractionDigits=3
分数.AlwaysShowsCimalSeparator=false
让分数部分=NSNumber(值:秒。截断余数(dividingBy:1))
返回组件。字符串(起始:秒)!+分数。字符串(起始:分数部分)!
}
打印(Locale.current)//ru\u ru(current)
print(format(600.5))//10:00,5您使用哪种方法格式化什么?Willeke:唯一接受参数1.5
:字符串的方法(来自: 时间时间间隔)
。时间间隔在内部转换为NSDateComponents。威勒克:是的,我假设是这样。这是他们实现它的合乎逻辑的方式。那么?@Daniel很遗憾,从iOS 13.3开始,这似乎是真的。我还没有找到从DateComponentFormter获得“1.5秒”本地化输出的方法。它将只输出“1.5”。超级讨厌。