-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2e10d03
commit 53fe327
Showing
2 changed files
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
//********************************************************************************************************************** | ||
// | ||
// String+Numeric.swift | ||
// Helper function for numeric parsing | ||
// Copyright ©2019 Peter Baumgartner. All rights reserved. | ||
// | ||
//********************************************************************************************************************** | ||
|
||
|
||
import Foundation | ||
|
||
|
||
//---------------------------------------------------------------------------------------------------------------------- | ||
|
||
|
||
// MARK: - | ||
|
||
public extension String | ||
{ | ||
|
||
/// Strips all non numeric charactef from a string. This includes leading and trailing whitespaces, as well | ||
/// as any letters that make up units. En example would be "100%" -> "100". The resulting string can be easily | ||
/// converted to an Int or Double. | ||
|
||
func strippingNonNumericCharacters() -> String | ||
{ | ||
let numbericCharacters = Set("0123456789.,") | ||
return self.filter { numbericCharacters.contains($0) } | ||
} | ||
|
||
var intValue : Int? | ||
{ | ||
return Int(self.strippingNonNumericCharacters()) | ||
} | ||
|
||
var floatValue : Float? | ||
{ | ||
return Float(self.strippingNonNumericCharacters()) | ||
} | ||
|
||
var doubleValue : Double? | ||
{ | ||
return Double(self.strippingNonNumericCharacters()) | ||
} | ||
} | ||
|
||
|
||
//---------------------------------------------------------------------------------------------------------------------- |