stringify_float 1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +64 -0
- data/lib/project/stringify_float.rb +45 -0
- data/lib/stringify_float.rb +10 -0
- metadata +61 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ab0d39fa1988ef8dbf83aae85050fe77a347200b
|
4
|
+
data.tar.gz: da5375985866fab54af2a54011f3c7f061169d88
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: dcd1728dfe0b5c8f07d69b318f5a823f0f560c939fe19941faf09d21d3cb93599442c9e6b80c366b56576c3722719fc870a68f42056c9bf0aa78fd1dda1dea2d
|
7
|
+
data.tar.gz: d2021b6e85812f16f4ed94b769d1779764a87a20caae8570fc55bc55014630a258d46d2328fbc1f856801c3a3f306933148f6097605196cbaeb82065aced23f6
|
data/README.md
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
[![Build Status](https://travis-ci.org/skandragon/stringify_float.png?branch=master)](https://travis-ci.org/skandragon/stringify_float)
|
2
|
+
|
3
|
+
# stringify_float
|
4
|
+
|
5
|
+
RubyMotion on 32-bit iOS devices suffers from poor floating point support:
|
6
|
+
|
7
|
+
(main)> 1.2
|
8
|
+
=> 1.19999980926514
|
9
|
+
|
10
|
+
This causes no end of problem with Core Data, and other uses where we really
|
11
|
+
want more precision than RubyMotion currently provides. Plus, generally
|
12
|
+
these values are converted to and from strings for presentation or editing.
|
13
|
+
These are convience functions to ease this use case as well.
|
14
|
+
|
15
|
+
## Installation
|
16
|
+
|
17
|
+
Add this line to your application's Gemfile:
|
18
|
+
|
19
|
+
gem 'stringify_float'
|
20
|
+
|
21
|
+
And then execute:
|
22
|
+
|
23
|
+
$ bundle
|
24
|
+
|
25
|
+
Or install it yourself as:
|
26
|
+
|
27
|
+
$ gem install stringify_float
|
28
|
+
|
29
|
+
## Usage
|
30
|
+
|
31
|
+
class Model
|
32
|
+
include StringifyFloat
|
33
|
+
|
34
|
+
attr_accessor :cost
|
35
|
+
stringify_float :cost
|
36
|
+
|
37
|
+
attr_accessor :quantity
|
38
|
+
stringify_float :quantity, precision: 1
|
39
|
+
end
|
40
|
+
|
41
|
+
>>> x = Model.new
|
42
|
+
>>> x.stringifiedCost = "123.45"
|
43
|
+
>>> x.stringifiedCost
|
44
|
+
"123.45"
|
45
|
+
|
46
|
+
>>> x.stringifiedQuantity = "123.45"
|
47
|
+
>>> x.stringifiedQuantity
|
48
|
+
"123.4"
|
49
|
+
|
50
|
+
"precision" specifies how many decimal places should be stored. This should
|
51
|
+
be greater than 1, and as many positions as you wish. Returned strings are
|
52
|
+
always 0-padded, so "123" becomes "123.00" (using the default precision).
|
53
|
+
|
54
|
+
The underlying data type limits how large a given integer can be. If
|
55
|
+
using Core Data, an integer32 can store values up to 21,474,836.48 using the
|
56
|
+
default precision of 2.
|
57
|
+
|
58
|
+
## Contributing
|
59
|
+
|
60
|
+
1. Fork it
|
61
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
62
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
63
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
64
|
+
5. Create new Pull Request
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module StringifyFloat
|
2
|
+
def self.included(base)
|
3
|
+
base.extend StringifyFloat::ClassMethods
|
4
|
+
end
|
5
|
+
|
6
|
+
module ClassMethods
|
7
|
+
def stringify_float(name, opts = {})
|
8
|
+
opts = {
|
9
|
+
precision: 2,
|
10
|
+
}.merge(opts)
|
11
|
+
|
12
|
+
precision = opts[:precision]
|
13
|
+
multiplier = 10 ** precision
|
14
|
+
|
15
|
+
name_titleized = name.to_s.dup
|
16
|
+
name_titleized[0] = name_titleized[0].upcase
|
17
|
+
|
18
|
+
define_method("stringified#{name_titleized}") do
|
19
|
+
value = send("#{name}")
|
20
|
+
if value
|
21
|
+
value = value.to_i
|
22
|
+
"#{value / multiplier}.#{'%02d' % (value % multiplier)}"
|
23
|
+
else
|
24
|
+
""
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
define_method("stringified#{name_titleized}=") do |newValue|
|
29
|
+
newValue = newValue.to_s
|
30
|
+
if newValue.nil? or newValue.length == 0
|
31
|
+
value = nil
|
32
|
+
else
|
33
|
+
int_string, fraction_string = newValue.split('.')
|
34
|
+
value = int_string.to_i * multiplier
|
35
|
+
if fraction_string
|
36
|
+
fraction_string += "0" while fraction_string.length < precision
|
37
|
+
fraction = fraction_string[0..(precision - 1)].to_i
|
38
|
+
value += fraction
|
39
|
+
end
|
40
|
+
end
|
41
|
+
send("#{name}=", value)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
unless defined?(Motion::Project::Config)
|
4
|
+
raise "This file must be required within a RubyMotion project Rakefile."
|
5
|
+
end
|
6
|
+
|
7
|
+
lib_dir_path = File.dirname(File.expand_path(__FILE__))
|
8
|
+
Motion::Project::App.setup do |app|
|
9
|
+
app.files.unshift(Dir.glob(File.join(lib_dir_path, "project/**/*.rb")))
|
10
|
+
end
|
metadata
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: stringify_float
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '1.0'
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Michael Graff
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-03-07 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rake
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description: Work around poor floating point support with RubyMotion and Core Data
|
28
|
+
email:
|
29
|
+
- explorer@flame.org
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- README.md
|
35
|
+
- lib/project/stringify_float.rb
|
36
|
+
- lib/stringify_float.rb
|
37
|
+
homepage: https://github.com/skandragon/stringify_float
|
38
|
+
licenses:
|
39
|
+
- MIT
|
40
|
+
metadata: {}
|
41
|
+
post_install_message:
|
42
|
+
rdoc_options: []
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0'
|
50
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
requirements: []
|
56
|
+
rubyforge_project:
|
57
|
+
rubygems_version: 2.2.0
|
58
|
+
signing_key:
|
59
|
+
specification_version: 4
|
60
|
+
summary: Work around poor floating point support in RubyMotion + Core Data
|
61
|
+
test_files: []
|