DecimalFormatter.swift 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // DecimalFormatter.swift
  2. // Eureka ( https://github.com/xmartlabs/Eureka )
  3. //
  4. // Copyright (c) 2016 Xmartlabs SRL ( http://xmartlabs.com )
  5. //
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in
  15. // all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. // THE SOFTWARE.
  24. import Foundation
  25. import UIKit
  26. /// A custom formatter for numbers with two digits after the decimal mark
  27. open class DecimalFormatter: NumberFormatter, FormatterProtocol {
  28. /// Creates the formatter with 2 Fraction Digits, Locale set to .current and .decimal NumberFormatter.Style
  29. public override init() {
  30. super.init()
  31. locale = Locale.current
  32. numberStyle = .decimal
  33. minimumFractionDigits = 2
  34. maximumFractionDigits = 2
  35. }
  36. required public init?(coder aDecoder: NSCoder) {
  37. fatalError("init(coder:) has not been implemented")
  38. }
  39. /// Creates an NSNumber from the given String
  40. /// - Parameter obj: Pointer to NSNumber object to assign
  41. /// - Parameter string: String with number assumed to have the configured min. fraction digits.
  42. /// - Parameter rangep: Unused range parameter
  43. override open func getObjectValue(_ obj: AutoreleasingUnsafeMutablePointer<AnyObject?>?, for string: String, range rangep: UnsafeMutablePointer<NSRange>?) throws {
  44. guard obj != nil else { return }
  45. let str = string.components(separatedBy: CharacterSet.decimalDigits.inverted).joined(separator: "")
  46. // Recover the number from the string in a way that forces the formatter's fraction digits
  47. // numberWithoutDecimals / 10 ^ minimumFractionDigits
  48. obj?.pointee = NSNumber(value: (Double(str) ?? 0.0)/Double(pow(10.0, Double(minimumFractionDigits))))
  49. }
  50. open func getNewPosition(forPosition position: UITextPosition, inTextInput textInput: UITextInput, oldValue: String?, newValue: String?) -> UITextPosition {
  51. return textInput.position(from: position, offset:((newValue?.count ?? 0) - (oldValue?.count ?? 0))) ?? position
  52. }
  53. }