From 99c8d6c8bf79985ff465a866674d789fdbaf1588 Mon Sep 17 00:00:00 2001 From: Andy Liu Date: Tue, 24 Sep 2024 18:01:31 +0700 Subject: [PATCH] Update for Embedded Swift Signed-off-by: Andy Liu --- .../Mission9_LCD/Sources/Mission9_LCD/LCD1602.swift | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/Examples/MakerKit/Mission9_LCD/Sources/Mission9_LCD/LCD1602.swift b/Examples/MakerKit/Mission9_LCD/Sources/Mission9_LCD/LCD1602.swift index c5cab4a..ce2f776 100644 --- a/Examples/MakerKit/Mission9_LCD/Sources/Mission9_LCD/LCD1602.swift +++ b/Examples/MakerKit/Mission9_LCD/Sources/Mission9_LCD/LCD1602.swift @@ -243,9 +243,11 @@ final public class LCD1602 { mul *= 10 } let expandValue = Int(num * Float(mul)) - write(x: x, y: y, String(Float(expandValue) / Float(mul))) + let str = getFloatString(Float(expandValue) / Float(mul)) + write(x: x, y: y, str) } else { - write(x: x, y: y, String(num)) + let str = getFloatString(num) + write(x: x, y: y, str) } } } @@ -279,3 +281,10 @@ extension LCD1602 { } } } + + +func getFloatString(_ num: Float) -> String { + let int = Int(num) + let frac = Int((num - Float(int)) * 100) + return "\(int).\(frac)" +} \ No newline at end of file