nepali_number 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 29cc199bf66bc17cf33aae0ff233060083e61ed53ea2a667d7568193bd0f2c8d
4
+ data.tar.gz: 6f711a7ba972a0630be4e984892366343c5e6c2baec04e7327030450607b5bab
5
+ SHA512:
6
+ metadata.gz: 723097d571768a73e7c4c25b2a973b825da7f82cef4a8caabb7c88e6d83b222d183ac654fb17baabcdaec9aa893688955ef86626d55cb77ed0661e13a7d9c152
7
+ data.tar.gz: 5ea57a0185510aa74423cf9648262abf949cd57f5c314055abf4089ce93a190f49c61dfc04230c3c9078be26076536fe88bd6696869cec82534fdd1da3acb8d1
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [0.1.0] - 2026-05-27
2
+
3
+ - Merge the richer Devanagari/Nepali-number implementation into `nepali_number`.
4
+ - Add humanized Nepali units, Devanagari digit conversion, Rails helpers, locale data, and Money integration.
5
+ - Keep `रु.` as the default Nepali currency symbol and support English or Devanagari digit output.
@@ -0,0 +1,10 @@
1
+ # Code of Conduct
2
+
3
+ "nepali_number" follows [The Ruby Community Conduct Guideline](https://www.ruby-lang.org/en/conduct) in all "collaborative space", which is defined as community communications channels (such as mailing lists, submitted patches, commit comments, etc.):
4
+
5
+ * Participants will be tolerant of opposing views.
6
+ * Participants must ensure that their language and actions are free of personal attacks and disparaging personal remarks.
7
+ * When interpreting the words and actions of others, participants should always assume good intentions.
8
+ * Behaviour which can be reasonably considered harassment will not be tolerated.
9
+
10
+ If you have any concerns about behaviour within this project, please contact us at ["zoras@users.noreply.github.com"](mailto:"zoras@users.noreply.github.com").
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2026 Saroj Maharjan (zoras)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,80 @@
1
+ # NepaliNumber
2
+
3
+ NepaliNumber formats numbers using the Nepali numbering system, where digits are grouped as thousands, lakhs, crores, and above. It also converts Devanagari digits, humanizes Nepali units, formats Nepali currency, and includes optional Rails helpers.
4
+
5
+ ## Installation
6
+
7
+ Install the gem and add it to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem "nepali_number"
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ### Number formatting
16
+
17
+ ```ruby
18
+ NepaliNumber.format(2870000)
19
+ # => "28,70,000"
20
+
21
+ NepaliNumber.format("123456.78")
22
+ # => "1,23,456.78"
23
+ ```
24
+
25
+ ### Currency
26
+
27
+ ```ruby
28
+ NepaliNumber.currency(100)
29
+ # => "रु. 100"
30
+
31
+ NepaliNumber.currency(1234567.89, nepali: true)
32
+ # => "रु. १२,३४,५६७.८९"
33
+ ```
34
+
35
+ ### Digit conversion
36
+
37
+ ```ruby
38
+ NepaliNumber.to_nepali_digits("12,34,567")
39
+ # => "१२,३४,५६७"
40
+
41
+ NepaliNumber.to_english_digits("१२,३४,५६७")
42
+ # => "12,34,567"
43
+ ```
44
+
45
+ ### Humanized units
46
+
47
+ ```ruby
48
+ NepaliNumber.human(100_000)
49
+ # => "1 लाख"
50
+
51
+ NepaliNumber.human(10_000_000)
52
+ # => "1 करोड"
53
+ ```
54
+
55
+ ### Rails
56
+
57
+ When Rails is loaded, NepaliNumber adds explicit helpers:
58
+
59
+ ```erb
60
+ <%= number_to_nepali(2870000) %>
61
+ <%= number_to_nepali_currency(100) %>
62
+ ```
63
+
64
+ ## Development
65
+
66
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
67
+
68
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
69
+
70
+ ## Contributing
71
+
72
+ Bug reports and pull requests are welcome.
73
+
74
+ ## License
75
+
76
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
77
+
78
+ ## Code of Conduct
79
+
80
+ Everyone interacting in the NepaliNumber project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/zoras/nepali_number/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "minitest/test_task"
5
+
6
+ Minitest::TestTask.create
7
+
8
+ require "standard/rake"
9
+
10
+ task default: %i[test standard]
@@ -0,0 +1,19 @@
1
+ ne:
2
+ number:
3
+ currency:
4
+ format:
5
+ unit: "रु."
6
+ delimiter: ","
7
+ separator: "."
8
+ format: "%u %n"
9
+ human:
10
+ units:
11
+ unit: ""
12
+ thousand: "हजार"
13
+ lakh: "लाख"
14
+ crore: "करोड"
15
+ arab: "अर्ब"
16
+ kharba: "खर्ब"
17
+ neel: "नील"
18
+ padma: "पद्म"
19
+ shankha: "शंख"
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NepaliNumber
4
+ class Currency
5
+ class << self
6
+ def format(amount, symbol: "रु.", nepali: false, devanagari: nil, delimiter: ",", separator: ".", space: true)
7
+ formatted = Formatter.format(amount, delimiter: delimiter, separator: separator)
8
+ return nil if formatted.nil?
9
+
10
+ formatted = DigitConverter.to_nepali(formatted) if devanagari.nil? ? nepali : devanagari
11
+ return formatted if symbol.nil? || symbol.empty?
12
+
13
+ joiner = space ? " " : ""
14
+ "#{symbol}#{joiner}#{formatted}"
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NepaliNumber
4
+ class DigitConverter
5
+ DEVANAGARI_DIGITS = {
6
+ "0" => "०",
7
+ "1" => "१",
8
+ "2" => "२",
9
+ "3" => "३",
10
+ "4" => "४",
11
+ "5" => "५",
12
+ "6" => "६",
13
+ "7" => "७",
14
+ "8" => "८",
15
+ "9" => "९"
16
+ }.freeze
17
+
18
+ ENGLISH_DIGITS = DEVANAGARI_DIGITS.invert.freeze
19
+
20
+ class << self
21
+ def to_nepali(number)
22
+ number.to_s.gsub(/\d/, DEVANAGARI_DIGITS)
23
+ end
24
+ alias_method :to_devanagari, :to_nepali
25
+
26
+ def to_english(number)
27
+ number.to_s.gsub(/[०-९]/, ENGLISH_DIGITS)
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NepaliNumber
4
+ class Formatter
5
+ NUMERIC_PATTERN = /\A([+-]?)(\d+)(?:\.(\d+))?\z/
6
+
7
+ class << self
8
+ def format(number, delimiter: ",", separator: ".")
9
+ return nil if number.nil?
10
+
11
+ raw = normalize(number)
12
+ match = NUMERIC_PATTERN.match(raw)
13
+ return raw unless match
14
+
15
+ sign = match[1]
16
+ integer = match[2]
17
+ decimal = match[3]
18
+
19
+ output = "#{sign}#{group_integer(integer, delimiter)}"
20
+ output += "#{separator}#{decimal}" if decimal
21
+ output
22
+ end
23
+
24
+ private
25
+
26
+ def normalize(number)
27
+ return number.to_s("F") if number.is_a?(BigDecimal)
28
+
29
+ number.to_s.delete(",")
30
+ end
31
+
32
+ def group_integer(integer, delimiter)
33
+ return integer if integer.length <= 3
34
+
35
+ prefix = integer[0...-3].reverse.scan(/.{1,2}/).map(&:reverse).reverse
36
+ [*prefix, integer[-3, 3]].join(delimiter)
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NepaliNumber
4
+ class Humanizer
5
+ UNITS = [
6
+ [10**17, "शंख"],
7
+ [10**15, "पद्म"],
8
+ [10**13, "नील"],
9
+ [10**11, "खर्ब"],
10
+ [10**9, "अर्ब"],
11
+ [10**7, "करोड"],
12
+ [10**5, "लाख"],
13
+ [10**3, "हजार"],
14
+ [10**2, "सय"]
15
+ ].freeze
16
+
17
+ class << self
18
+ def human(number, precision: 2, strip_insignificant_zeros: true, units: UNITS)
19
+ raise ArgumentError, "number is required" if number.nil?
20
+
21
+ value = BigDecimal(number.to_s)
22
+ sign = value.negative? ? "-" : ""
23
+ absolute = value.abs
24
+
25
+ unit_value, unit_name = units.find { |candidate_value, _| absolute >= candidate_value }
26
+ return "#{sign}#{integer_string(absolute)}" unless unit_value
27
+
28
+ scaled = absolute / unit_value
29
+ scaled_text =
30
+ if scaled.frac.zero?
31
+ integer_string(scaled)
32
+ else
33
+ decimal_string(scaled, precision, strip_insignificant_zeros)
34
+ end
35
+
36
+ "#{sign}#{scaled_text} #{unit_name}"
37
+ end
38
+
39
+ private
40
+
41
+ def decimal_string(number, precision, strip_insignificant_zeros)
42
+ text = format("%.#{precision}f", number)
43
+ return text unless strip_insignificant_zeros
44
+
45
+ text.sub(/\.?0+\z/, "")
46
+ end
47
+
48
+ def integer_string(number)
49
+ number.to_i.to_s
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NepaliNumber
4
+ module MoneyIntegration
5
+ module_function
6
+
7
+ def install!
8
+ return false unless defined?(Money)
9
+
10
+ register_npr_currency
11
+ true
12
+ end
13
+
14
+ def format(money, nepali: false, devanagari: nil, symbol: nil)
15
+ unless defined?(Money) && money.is_a?(Money)
16
+ raise ArgumentError, "expected a Money object"
17
+ end
18
+
19
+ currency = money.currency
20
+ currency_symbol = symbol || default_symbol_for(currency)
21
+ value = money.respond_to?(:to_d) ? money.to_d : BigDecimal(money.to_f.to_s)
22
+ value = value.to_i if value.respond_to?(:frac) && value.frac.zero?
23
+
24
+ Currency.format(value, symbol: currency_symbol, nepali: nepali, devanagari: devanagari)
25
+ end
26
+
27
+ def register_npr_currency
28
+ return unless defined?(Money::Currency)
29
+ return if npr_registered?
30
+
31
+ Money::Currency.register(
32
+ iso_code: "NPR",
33
+ name: "Nepalese Rupee",
34
+ symbol: "रु.",
35
+ subunit: "Paisa",
36
+ subunit_to_unit: 100
37
+ )
38
+ rescue
39
+ false
40
+ end
41
+
42
+ def npr_registered?
43
+ return false unless defined?(Money::Currency)
44
+
45
+ Money::Currency.find("NPR")
46
+ true
47
+ rescue
48
+ false
49
+ end
50
+
51
+ def default_symbol_for(currency)
52
+ return "रु." if currency.iso_code == "NPR"
53
+
54
+ currency.symbol || currency.iso_code
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NepaliNumber
4
+ module RailsHelper
5
+ def number_to_nepali(number, **options)
6
+ NepaliNumber.format(number, **options)
7
+ end
8
+
9
+ def number_to_nepali_currency(number, **options)
10
+ NepaliNumber.currency(number, **options)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NepaliNumber
4
+ class Railtie < Rails::Railtie
5
+ initializer "nepali_number.load_locale" do
6
+ I18n.load_path << File.expand_path("../../config/locales/ne.yml", __dir__) if defined?(I18n)
7
+ end
8
+
9
+ initializer "nepali_number.helpers" do
10
+ ActiveSupport.on_load(:action_view) do
11
+ include NepaliNumber::RailsHelper
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NepaliNumber
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bigdecimal"
4
+
5
+ require_relative "nepali_number/version"
6
+ require_relative "nepali_number/formatter"
7
+ require_relative "nepali_number/humanizer"
8
+ require_relative "nepali_number/digit_converter"
9
+ require_relative "nepali_number/currency"
10
+ require_relative "nepali_number/money_integration"
11
+ require_relative "nepali_number/rails_helper"
12
+
13
+ module NepaliNumber
14
+ class Error < StandardError; end
15
+
16
+ class << self
17
+ def format(number, **options)
18
+ Formatter.format(number, **options)
19
+ end
20
+
21
+ def human(number, **options)
22
+ Humanizer.human(number, **options)
23
+ end
24
+
25
+ def to_nepali_digits(number)
26
+ DigitConverter.to_nepali(number)
27
+ end
28
+ alias_method :to_devanagari_digits, :to_nepali_digits
29
+
30
+ def to_english_digits(number)
31
+ DigitConverter.to_english(number)
32
+ end
33
+
34
+ def currency(amount, **options)
35
+ Currency.format(amount, **options)
36
+ end
37
+ end
38
+ end
39
+
40
+ require_relative "nepali_number/railtie" if defined?(Rails::Railtie)
@@ -0,0 +1,10 @@
1
+ module NepaliNumber
2
+ VERSION: String
3
+
4
+ def self.format: (untyped number, ?delimiter: String delimiter, ?separator: String separator) -> String?
5
+ def self.human: (untyped number, ?precision: Integer precision, ?strip_insignificant_zeros: bool strip_insignificant_zeros, ?units: untyped units) -> String
6
+ def self.to_nepali_digits: (untyped number) -> String
7
+ def self.to_devanagari_digits: (untyped number) -> String
8
+ def self.to_english_digits: (untyped number) -> String
9
+ def self.currency: (untyped amount, ?symbol: String? symbol, ?nepali: bool nepali, ?devanagari: bool? devanagari, ?delimiter: String delimiter, ?separator: String separator, ?space: bool space) -> String?
10
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nepali_number
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Saroj Maharjan (zoras)
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: i18n
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '1.0'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: '1.0'
26
+ description: Formats numbers with Nepali lakh/crore grouping, converts Devanagari
27
+ digits, humanizes Nepali units, and formats Nepali currency.
28
+ email:
29
+ - zoras@users.noreply.github.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - CHANGELOG.md
35
+ - CODE_OF_CONDUCT.md
36
+ - LICENSE.txt
37
+ - README.md
38
+ - Rakefile
39
+ - config/locales/ne.yml
40
+ - lib/nepali_number.rb
41
+ - lib/nepali_number/currency.rb
42
+ - lib/nepali_number/digit_converter.rb
43
+ - lib/nepali_number/formatter.rb
44
+ - lib/nepali_number/humanizer.rb
45
+ - lib/nepali_number/money_integration.rb
46
+ - lib/nepali_number/rails_helper.rb
47
+ - lib/nepali_number/railtie.rb
48
+ - lib/nepali_number/version.rb
49
+ - sig/nepali_number.rbs
50
+ homepage: https://github.com/zoras/nepali_number
51
+ licenses:
52
+ - MIT
53
+ metadata:
54
+ allowed_push_host: https://rubygems.org
55
+ homepage_uri: https://github.com/zoras/nepali_number
56
+ source_code_uri: https://github.com/zoras/nepali_number/tree/main
57
+ changelog_uri: https://github.com/zoras/nepali_number/blob/main/CHANGELOG.md
58
+ rdoc_options: []
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: 3.2.0
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements: []
72
+ rubygems_version: 4.0.12
73
+ specification_version: 4
74
+ summary: Nepali number formatting, humanization, digit conversion, and currency helpers.
75
+ test_files: []