Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-9269] NumberFormatter().string(from:Decimal) Returns Wrong Value on Linux #3588

Closed
swift-ci opened this issue Nov 15, 2018 · 1 comment
Closed

Comments

@swift-ci
Copy link
Contributor

Previous ID SR-9269
Radar None
Original Reporter Shadrick (JIRA User)
Type Bug
Status Resolved
Resolution Done
Environment

Results - Docker Official - Swift 4.2 ubuntu:16.04

Additional Detail from JIRA
Votes 0
Component/s Foundation
Labels Bug
Assignee None
Priority Medium

md5: 1fd45dd10d039996471fbbd9ab9b2857

Issue Description:

NumberFormatter().string(from:Decimal) returns the last element of the calc rather than 0.00. This happens in 4.1.3 and 4.2 Linux, but works fine on Mac OS.

Here is a simple test script that can be run on Mac OS and Linux with different results:

#!/usr/bin/swift

import Foundation

print("Simple Test\nDecimal\t\tFormatted")

let nf = NumberFormatter()
nf.locale = Locale(identifier: "en_US")
nf.numberStyle = .decimal
nf.minimumFractionDigits = 2
nf.maximumFractionDigits = 2

let x = Decimal(10.5)
let y = Decimal(9.0)
let z = Decimal(1.5)

let a = x - y - z
let b = x - z - y

print("x=Decimal(10.5):Formatted \(nf.string(from: NSDecimalNumber(decimal:x)) ?? "")")
print("y=Decimal(9.0):\tFormatted \(nf.string(from: NSDecimalNumber(decimal:y)) ?? "")")
print("z=Decimal(1.5):\tFormatted \(nf.string(from: NSDecimalNumber(decimal:z)) ?? "")")
print("x - y - z = \(a):\tFormatted \(nf.string(from: NSDecimalNumber(decimal:a)) ?? "")")
print("x - z - y = \(b):\tFormatted \(nf.string(from: NSDecimalNumber(decimal:b)) ?? "")")

Results - Docker Official - Swift 4.2 ubuntu:16.04

Simple Test
Decimal Formatted 
x=Decimal(10.5):Formatted 10.50 
y=Decimal(9.0): Formatted 9.00 
z=Decimal(1.5): Formatted 1.50 
x - y - z = 0: Formatted 1.50 
x - z - y = 0: Formatted 9.00

Results Swift 4.1.2 MacOS 10.13.6

Simple Test 
Decimal Formatted 
x=Decimal(10.5):Formatted 10.50 
y=Decimal(9.0): Formatted 9.00 
z=Decimal(1.5): Formatted 1.50 
x - y - z = 0: Formatted 0.00 
x - z - y = 0: Formatted 0.00
@spevans
Copy link
Collaborator

spevans commented Nov 15, 2018

This was fixed in #1759

$ ~/swift-DEVELOPMENT-SNAPSHOT-2018-11-13-a-ubuntu18.04/usr/bin/swift sr_9269.swift 
Simple Test
Decimal         Formatted
x=Decimal(10.5):Formatted 10.50
y=Decimal(9.0): Formatted 9.00
z=Decimal(1.5): Formatted 1.50
x - y - z = 0:  Formatted 0.00
x - z - y = 0:  Formatted 0.00

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@shahmishal shahmishal transferred this issue from apple/swift May 5, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants