String, 소수점 두 자리 처리
cmd + shift + 0 눌러 String 검색하면 워낙 자세히 나와 있어 포스팅할 필요성은 없지만 행여나 키워드에 걸릴까 올린다.
보통은 직접대입한다.
GS.s.tp1?.labelBloodSugar.text = String(describing: VD.s.vdTrendStat?.cgmAvg ?? 0)
.
+ 연산자로 합칠 수 있다.
let combinedTextAvgStdev : String = String(describing : VD.s.vdTrendStat?.cgmAvg ?? 0) + "±" + String(describing : VD.s.vdTrendStat?.cgmStdev ?? 0)
GS.s.tp1?.labelBloodSugar.text = combinedTextAvgStdev
.
소수점 두자리 처리는 일단 원하는 소수점 수만큼 곱한 다음 round로 필요없는 소수점 잘라내고 다시 소수점으로 내린다.
let numberOfPlaces = 2.0
let multiplier = pow(10.0, numberOfPlaces)
var num = Double(VD.s.vdTrendStat?.cgmAvg ?? 0)
var rounded = round(num * multiplier) / multiplier
var combinedTextAvgStdev : String = String(describing : rounded) + "±" + String(describing : VD.s.vdTrendStat?.cgmStdev ?? 0)
GS.s.tp1?.labelBloodSugar.text = combinedTextAvgStdev
.
쓸 때는
GS.s.tp1?.labelUpperValue.text = String(describing: ((VD.s.vdTrendStat?.arrCgmCount.filter { $0.key == "high" })?.first?.value ?? 0)