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

Now display a localized decimal separator. #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions Calculator.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 10pm;
DEVELOPMENT_ASSET_PATHS = "\"Calculator/Preview Content\"";
DEVELOPMENT_TEAM = 8A4DL788W9;
DEVELOPMENT_TEAM = 6389FT2N69;
ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = Calculator/Info.plist;
Expand All @@ -568,7 +568,7 @@
SUPPORTS_MACCATALYST = YES;
SWIFT_EMIT_LOC_STRINGS = YES;
SWIFT_ENFORCE_EXCLUSIVE_ACCESS = off;
SWIFT_OBJC_BRIDGING_HEADER = /Users/joachim/Desktop/Calculator/dist/include/bridge.h;
SWIFT_OBJC_BRIDGING_HEADER = dist/include/bridge.h;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2,6";
};
Expand All @@ -584,7 +584,7 @@
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 10pm;
DEVELOPMENT_ASSET_PATHS = "\"Calculator/Preview Content\"";
DEVELOPMENT_TEAM = 8A4DL788W9;
DEVELOPMENT_TEAM = 6389FT2N69;
ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = Calculator/Info.plist;
Expand All @@ -607,7 +607,7 @@
SUPPORTS_MACCATALYST = YES;
SWIFT_EMIT_LOC_STRINGS = YES;
SWIFT_ENFORCE_EXCLUSIVE_ACCESS = off;
SWIFT_OBJC_BRIDGING_HEADER = /Users/joachim/Desktop/Calculator/dist/include/bridge.h;
SWIFT_OBJC_BRIDGING_HEADER = dist/include/bridge.h;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2,6";
};
Expand All @@ -620,7 +620,7 @@
BUNDLE_LOADER = "$(TEST_HOST)";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_TEAM = 8A4DL788W9;
DEVELOPMENT_TEAM = 6389FT2N69;
GENERATE_INFOPLIST_FILE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 15.0;
LD_RUNPATH_SEARCH_PATHS = (
Expand All @@ -645,7 +645,7 @@
BUNDLE_LOADER = "$(TEST_HOST)";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_TEAM = 8A4DL788W9;
DEVELOPMENT_TEAM = 6389FT2N69;
GENERATE_INFOPLIST_FILE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 15.0;
LD_RUNPATH_SEARCH_PATHS = (
Expand All @@ -669,7 +669,7 @@
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_TEAM = 8A4DL788W9;
DEVELOPMENT_TEAM = 6389FT2N69;
GENERATE_INFOPLIST_FILE = YES;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
Expand All @@ -692,7 +692,7 @@
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_TEAM = 8A4DL788W9;
DEVELOPMENT_TEAM = 6389FT2N69;
GENERATE_INFOPLIST_FILE = YES;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
Expand Down
98 changes: 98 additions & 0 deletions Calculator.xcodeproj/xcshareddata/xcschemes/Calculator.xcscheme
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1420"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "AA08C7BD272EDA48001B8575"
BuildableName = "Better Calc.app"
BlueprintName = "Calculator"
ReferencedContainer = "container:Calculator.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "AA08C7CD272EDA4A001B8575"
BuildableName = "CalculatorTests.xctest"
BlueprintName = "CalculatorTests"
ReferencedContainer = "container:Calculator.xcodeproj">
</BuildableReference>
</TestableReference>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "AA08C7D7272EDA4A001B8575"
BuildableName = "CalculatorUITests.xctest"
BlueprintName = "CalculatorUITests"
ReferencedContainer = "container:Calculator.xcodeproj">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "AA08C7BD272EDA48001B8575"
BuildableName = "Better Calc.app"
BlueprintName = "Calculator"
ReferencedContainer = "container:Calculator.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "AA08C7BD272EDA48001B8575"
BuildableName = "Better Calc.app"
BlueprintName = "Calculator"
ReferencedContainer = "container:Calculator.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
3 changes: 2 additions & 1 deletion Calculator/Brain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import SwiftUI
class Brain: ObservableObject {
@Published var scrollViewTarget: Int? = nil
var messageToUser: String? = nil
let separator = Locale.current.decimalSeparator
private var n = NumberStack()
private var operatorStack = OperatorStack()
private var displayData: DisplayData = DisplayData()
Expand Down Expand Up @@ -193,7 +194,7 @@ class Brain: ObservableObject {
}
n.replaceLast(with: Number(gmp))
}
} else if symbol == "," {
} else if symbol == separator {
if pendingOperator != nil {
n.append(Number("0"))
pendingOperator = nil
Expand Down
7 changes: 4 additions & 3 deletions Calculator/DisplayData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class DisplayData: ObservableObject {

func update(with number: Number, digitsInExpandedDisplay: Int = DisplayData.digitsInExpandedDisplay) {
let gmp: Gmp
let separator = Locale.current.decimalSeparator!
if let str = number.str {
if str.count <= DisplayData.digitsInOneLine {
set(str)
Expand Down Expand Up @@ -104,7 +105,7 @@ class DisplayData: ObservableObject {
/// can be displayed
var floatString = data.mantissa
let index = floatString.index(floatString.startIndex, offsetBy: data.exponent+1)
floatString.insert(",", at: index)
floatString.insert(contentsOf:separator, at: index)
if data.negative { floatString = "-" + floatString }
set(floatString)
return
Expand All @@ -113,7 +114,7 @@ class DisplayData: ObservableObject {
/// 0,xxxx
if -data.exponent < DisplayData.digitsInOneLine - 3 {
/// can be displayed
var floatString = "0,"
var floatString = "0" + separator
let zeroes = -data.exponent
for _ in 1..<zeroes {
floatString += "0"
Expand All @@ -129,7 +130,7 @@ class DisplayData: ObservableObject {
let exponent = "e\(data.exponent)"
var mantissa = data.mantissa
let indexOne = mantissa.index(mantissa.startIndex, offsetBy: 1)
mantissa.insert(",", at: indexOne)
mantissa.insert(contentsOf:separator, at: indexOne)
if mantissa.count <= 2 { mantissa += "0" } /// e.g. 1e16 -> 1,e16 -> 1,0e16
if data.negative { mantissa = "-" + mantissa }
set(Scientific(mantissa, exponent))
Expand Down
3 changes: 2 additions & 1 deletion Calculator/Gmp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ class Gmp: Equatable {
/// there is only ine initialzer that takes a string.
/// Implementing an initializer that accepts a double which is created from a string leads to a loss of precision.
init(_ s: String) {
let s1 = s.replacingOccurrences(of: ",", with: ".")
let separator = Locale.current.decimalSeparator!
let s1 = s.replacingOccurrences(of: separator, with: ".")
mpfr_init2 (&mpfr, globalGmpSignificantBits)
mpfr_set_str (&mpfr, s1, 10, MPFR_RNDN)
}
Expand Down
5 changes: 3 additions & 2 deletions Calculator/Number.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Foundation
class Number: CustomDebugStringConvertible {
private var _str: String?
private var _gmp: Gmp?
let separator = Locale.current.decimalSeparator!

var isStr: Bool { _str != nil }
var str: String? { return _str }
Expand Down Expand Up @@ -67,9 +68,9 @@ class Number: CustomDebugStringConvertible {

func addComma() {
if str == nil {
_str = "0,"
_str = "0" + separator
} else {
if !_str!.contains(",") { _str!.append(",") }
if !_str!.contains(separator) { _str!.append(separator) }
}
}

Expand Down
3 changes: 2 additions & 1 deletion Calculator/NumberKeys.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import SwiftUI
struct NumberKeys: View {
@ObservedObject var brain: Brain
let t: TE
let separator = Locale.current.decimalSeparator!

var body: some View {
VStack(spacing: t.spaceBetweenkeys) {
Expand Down Expand Up @@ -40,7 +41,7 @@ struct NumberKeys: View {
}
HStack(spacing: t.spaceBetweenkeys) {
Key("0", requiresValidNuber: false, brain: brain, t: t, keyProperties: t.digits_0)
Key(",", requiresValidNuber: false, brain: brain, t: t, keyProperties: t.digits_1_9)
Key(separator, requiresValidNuber: false, brain: brain, t: t, keyProperties: t.digits_1_9)
Key("=", requiresValidNuber: true, brain: brain, t: t, keyProperties: t.colorOpProperties)
}
}
Expand Down
Loading