Swiftui Textfield四舍五入输入问题

发布于 2025-01-28 09:43:24 字数 1098 浏览 2 评论 0原文

我有一个textfield圆形问题。如果我输入1到1.49的任何地方都很好。如果我输入1.5或向上(至1.999),则该字段最多可达2个。如何将其保留为我的输入。如果我输入1.73,我希望它保留1.73,而不是2。这是代码:

                        VStack {
                            let lineOneProxy = Binding<String>(
                                get: { if self.lineOne == 0 {
                                    return ""
                                } else {
                                    return String(format: "%.0f", Double(self.lineOne)) }
                                },
                                set: { if let value = NumberFormatter().number(from: $0) {
                                    self.lineOne = value.doubleValue }
                                }
                            )
                            TextField("ENTER", text: lineOneProxy.max(5))
                        }

这是我设置NumberFormatter的方式:

extension NumberFormatter {
    convenience init(numberStyle: Style, locale: Locale = .current) {
        self.init()
        self.locale = locale
        self.numberStyle = numberStyle
    }
}

I'm having a textfield rounding issue. If I input anywhere from 1 to 1.49 all is good. If I input 1.5 or up (to 1.999) the field rounds up to 2. How do I keep this as what I input. If I input 1.73 I want it to remain 1.73, not 2. Here's the code:

                        VStack {
                            let lineOneProxy = Binding<String>(
                                get: { if self.lineOne == 0 {
                                    return ""
                                } else {
                                    return String(format: "%.0f", Double(self.lineOne)) }
                                },
                                set: { if let value = NumberFormatter().number(from: $0) {
                                    self.lineOne = value.doubleValue }
                                }
                            )
                            TextField("ENTER", text: lineOneProxy.max(5))
                        }

This is how I have NumberFormatter set up:

extension NumberFormatter {
    convenience init(numberStyle: Style, locale: Locale = .current) {
        self.init()
        self.locale = locale
        self.numberStyle = numberStyle
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

西瑶 2025-02-04 09:43:24

因为那是您告诉它要做的。在此行中:

return String(format: "%.0f", Double(self.lineOne)) }

指定%。0F表示以NO(0)分数数字为单位的一半汇总。

给定一个双value = 1234.12345

String(format: "%.0f", value)  // 1234
String(format: "%.1f", value)  // 1234.1
String(format: "%.2f", value)  // 1234.12
String(format: "%.3f", value)  // 1234.123
String(format: "%.4f", value)  // 1234.1235 - The 4 rounded up due to the trailing 5

编辑:

您问为什么要获得值。答案很明显,如果您不想要这种行为,请不要使用``%.0f''格式化。您需要2个分数数字,使用您创建的格式化器并将其编辑为:

extension NumberFormatter {
    convenience init(numberStyle: Style, locale: Locale = .current) {
        self.init()
        self.locale = locale
        // Set the fractional digits here:
        self.maximumFractionDigits = 2
        self.numberStyle = numberStyle
    }
}

使用以下方式:

struct DoubleStringFormat: View {

    @State var value = 1234.12345

    var body: some View {
        VStack {
            HStack {
                Text("Enter Value: ")
                TextField("", value: $value, format: .number)
                    .background(Color.gray.opacity(0.2))
                    .fixedSize()
            }
            Text(String(format: "%.0f", value))
            Text(String(format: "%.1f", value))
            Text(String(format: "%.2f", value))
            Text(String(format: "%.3f", value))
            Text(String(format: "%.4f", value))
            // Use your NumberFormatter here:
            Text(NumberFormatter(numberStyle: .decimal).string(from: value as NSNumber) ?? "")
        }
    }
}

Because that is what you told it to do. In this line:

return String(format: "%.0f", Double(self.lineOne)) }

specifying %.0f means to round up at half with no(0) fractional digits.

Given a double value = 1234.12345,

String(format: "%.0f", value)  // 1234
String(format: "%.1f", value)  // 1234.1
String(format: "%.2f", value)  // 1234.12
String(format: "%.3f", value)  // 1234.123
String(format: "%.4f", value)  // 1234.1235 - The 4 rounded up due to the trailing 5

Edit:

You asked why you were getting the value that you were. The answer is obvious, if you don't want the behavior,do not use the `%.0f" formatter. You never said what outcome you specifically wanted, but I am guessing 2 fractional digits from the way you worded some things. If you want 2 fractional digits, use the formatter you created and edit it to this:

extension NumberFormatter {
    convenience init(numberStyle: Style, locale: Locale = .current) {
        self.init()
        self.locale = locale
        // Set the fractional digits here:
        self.maximumFractionDigits = 2
        self.numberStyle = numberStyle
    }
}

Use it like:

struct DoubleStringFormat: View {

    @State var value = 1234.12345

    var body: some View {
        VStack {
            HStack {
                Text("Enter Value: ")
                TextField("", value: $value, format: .number)
                    .background(Color.gray.opacity(0.2))
                    .fixedSize()
            }
            Text(String(format: "%.0f", value))
            Text(String(format: "%.1f", value))
            Text(String(format: "%.2f", value))
            Text(String(format: "%.3f", value))
            Text(String(format: "%.4f", value))
            // Use your NumberFormatter here:
            Text(NumberFormatter(numberStyle: .decimal).string(from: value as NSNumber) ?? "")
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文