Class: Nio::Fmt
- Inherits:
-
Object
- Object
- Nio::Fmt
- Includes:
- StateEquivalent
- Defined in:
- lib/nio/fmt.rb,
lib/nio/fmt.rb,
lib/nio/sugar.rb
Overview
:stopdoc:
Defined Under Namespace
Classes: Error, InvalidFormat, InvalidOption
Constant Summary collapse
- CONV_FMT =
Intermediate conversion format for simplified conversion
Fmt.prec(:exact).rep('<','>','...',0).approx_mode(:simplify)
- CONV_FMT_STRICT =
Intermediate conversion format for exact conversion
Fmt.prec(:exact).rep('<','>','...',0).approx_mode(:exact)
- @@default_rounding_mode =
:even
- @@sci_fmt =
nil
- @@fmts =
{ :def=>Fmt.new.freeze }
Class Method Summary collapse
- .<<(x) ⇒ Object
- .>>(cls_txt) ⇒ Object
-
.[](tag) ⇒ Object
Retrieves a named format from the repository.
-
.[]=(tag, fmt_def) ⇒ Object
Assigns a format to a name in the formats repository.
-
.approx_mode(v) ⇒ Object
This is a shortcut to return a new default Fmt object and define approx_mode.
-
.base(b, uppercase = nil) ⇒ Object
This is a shortcut to create a new Fmt object and define the base.
-
.convert(x, type, mode = :approx) ⇒ Object
Numerical conversion: converts the quantity
x
to an object of classtype
. -
.default ⇒ Object
Returns the current default format.
-
.default=(fmt) ⇒ Object
Defines the current default format.
-
.default_rounding_mode ⇒ Object
Rounding mode used when not specified otherwise.
-
.default_rounding_mode=(m) ⇒ Object
The default rounding can be changed here; it starts with the value :even.
-
.grouping(grp = [3], grp_sep = nil) ⇒ Object
This is a shortcut to return a new default Fmt object and define the grouping as with #grouping().
-
.insignificant_digits(v = '#') ⇒ Object
This is a shortcut to return a new default Fmt object and define insignificant digits.
-
.mode(mode, ndig = nil, options = {}) ⇒ Object
This is a shortcut to return a new default Fmt object and define the formatting mode as with #mode().
-
.pad0s(w) ⇒ Object
This is a shortcut to create a new Fmt object and define numeric padding as with #pad0s().
-
.prec(ndig, mode = nil, options = {}) ⇒ Object
This is a shortcut to return a new default Fmt object and define the formatting mode as with #prec().
- .read(cls, txt) ⇒ Object
-
.rep(*params) ⇒ Object
This is a shortcut to return a new default Fmt object and define the repeating decimals mode as with #rep().
-
.sci_digits(v = -1)) ⇒ Object
This is a shortcut to return a new default Fmt object and define sci_digits.
-
.sep(dec_sep, grp_sep = nil, grp = nil) ⇒ Object
This is a shortcut to return a new default Fmt object and define the separators as with #sep().
-
.show_all_digits(v = true) ⇒ Object
This is a shortcut to return a new default Fmt object and define show_all_digits.
-
.show_plus(v = true) ⇒ Object
This is a shortcut to return a new default Fmt object and define show_plus.
-
.width(w, adj = nil, ch = nil) ⇒ Object
This is a shortcut to create a new Fmt object and define the width parameters as with #widht().
- .write(x) ⇒ Object
Instance Method Summary collapse
- #<<(x) ⇒ Object
- #>>(cls_txt) ⇒ Object
-
#approx_mode(mode) ⇒ Object
This defines the approximate mode (:only_sig, :exact, :simplify) just like the last parameter of #mode().
-
#approx_mode!(mode) ⇒ Object
This is the mutator version of #approx_mode().
-
#base(b, uppercase = nil) ⇒ Object
defines the numerical base; the second parameters forces the use of uppercase letters for bases greater than 10.
-
#base!(b, uppercase = nil) ⇒ Object
This is the mutator version of #base().
-
#get_all_digits? ⇒ Boolean
return the show_all_digits state.
-
#get_approx ⇒ Object
returns the approximate mode.
-
#get_base ⇒ Object
returns the base.
-
#get_base_digits(b = nil) ⇒ Object
returns the digit characters used for a base.
-
#get_base_uppercase? ⇒ Boolean
returns true if uppercase digits are used.
-
#get_exp_char(base) ⇒ Object
returns the exponent char used with the specified base.
-
#get_mode ⇒ Object
returns the formatting mode.
-
#get_ndig ⇒ Object
returns the precision (number of digits).
-
#get_round ⇒ Object
returns the rounding mode.
-
#grouping(grp = [3], grp_sep = nil) ⇒ Object
This defines the grouping of digits (which can also be defined in #sep().
-
#grouping!(grp = [3], grp_sep = nil) ⇒ Object
This is the mutator version of #grouping().
-
#initialize {|_self| ... } ⇒ Fmt
constructor
A new instance of Fmt.
-
#insignificant_digits(ch = '#') ⇒ Object
Defines a character to stand for insignificant digits when a specific number of digits has been requested greater than then number of significant digits (for approximate types).
-
#insignificant_digits!(ch = '#') ⇒ Object
This is the mutator version of #insignificant_digits().
-
#mode(mode, precision = nil, options = {}) ⇒ Object
Define the formatting mode.
-
#mode!(mode, precision = nil, options = {}) ⇒ Object
This is the mutator version of #mode().
-
#nio_read_formatted(txt) ⇒ Object
:nodoc:.
-
#nio_write_formatted(neutral) ⇒ Object
Method used internally to format a neutral numeral.
-
#pad0s(w) ⇒ Object
Defines the justification (as #width()) with the given width, internal mode and filling with zeros.
-
#pad0s!(w) ⇒ Object
This is the mutator version of #pad0s().
-
#prec(precision, mode = nil, options = {}) ⇒ Object
Defines the formatting mode like #mode() but using a different order of the first two parameters parameters, which is useful to change the precision only.
-
#prec!(precision, mode = :gen, options = {}) ⇒ Object
This is the mutator version of #prec().
- #read(cls, txt) ⇒ Object
-
#rep(*params) ⇒ Object
Defines the handling and notation for repeating numerals.
-
#rep!(*params) ⇒ Object
This is the mutator version of #rep().
-
#round!(neutral) ⇒ Object
round a neutral numeral according to the format options.
-
#sci_digits(n = -1)) ⇒ Object
Defines the number of significan digits before the radix separator in scientific notation.
-
#sci_digits!(n = -1)) ⇒ Object
This is the mutator version of #sci_digits().
-
#sep(dec_sep, grp_sep = nil, grp = nil) ⇒ Object
Defines the separators used in numerals.
-
#sep!(dec_sep, grp_sep = nil, grp = nil) ⇒ Object
This is the mutator version of #sep().
-
#show_all_digits(ad = true) ⇒ Object
This controls the display of the digits that are not necessary to specify the value unambiguosly (e.g. trailing zeros).
-
#show_all_digits!(ad = true) ⇒ Object
This is the mutator version of #show_all_digits().
-
#show_plus(sp = true) ⇒ Object
Controls the display of the sign for positive numbers.
-
#show_plus!(sp = true) ⇒ Object
This is the mutator version of #show_plus().
-
#width(w, adj = nil, ch = nil) ⇒ Object
Sets the justificaton width, mode and fill character.
-
#width!(w, adj = nil, ch = nil) ⇒ Object
This is the mutator version of #width().
- #write(x) ⇒ Object
Methods included from StateEquivalent
Constructor Details
#initialize {|_self| ... } ⇒ Fmt
Returns a new instance of Fmt.
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 |
# File 'lib/nio/fmt.rb', line 424 def initialize() @dec_sep = '.' @grp_sep = ',' @grp = [] @ndig = :exact @mode=:gen @round=Fmt.default_rounding_mode @all_digits = false @approx = :only_sig @non_sig = '' # marker for insignificant digits of inexact values e.g. '#','0' @sci_format = 1 # number of integral digits in the mantissa: -1 for all @show_plus = false @rep_begin = '<' @rep_end = '>' @rep_auto = '...' @rep_n = 2 @rep_in = true @width = 0 @fill_char = ' ' @adjust=:right @base_radix = 10 @base_uppercase = true @base_digits = DigitsDef.base(@base_radix, !@base_uppercase) @show_base = false @base_indicators = { 2=>'b', 8=>'o', 10=>'', 16=>'h', 0=>'r'} # 0: generic (used with radix) @base_prefix = false @nan_txt = 'NAN' @inf_txt = 'Infinity' yield self if block_given? end |
Class Method Details
.<<(x) ⇒ Object
44 45 46 |
# File 'lib/nio/sugar.rb', line 44 def Fmt.<<(x) x.nio_write end |
.>>(cls_txt) ⇒ Object
57 58 59 60 |
# File 'lib/nio/sugar.rb', line 57 def Fmt.>>(cls_txt) cls,txt = cls_txt cls.nio_read(txt) end |
.[](tag) ⇒ Object
Retrieves a named format from the repository.
1042 1043 1044 |
# File 'lib/nio/fmt.rb', line 1042 def self.[](tag) @@fmts[tag.to_sym] end |
.[]=(tag, fmt_def) ⇒ Object
Assigns a format to a name in the formats repository.
1038 1039 1040 |
# File 'lib/nio/fmt.rb', line 1038 def self.[]=(tag,fmt_def) @@fmts[tag.to_sym]=fmt_def.freeze end |
.approx_mode(v) ⇒ Object
This is a shortcut to return a new default Fmt object and define approx_mode
646 647 648 |
# File 'lib/nio/fmt.rb', line 646 def Fmt.approx_mode(v) Fmt.default.approx_mode(v) end |
.base(b, uppercase = nil) ⇒ Object
This is a shortcut to create a new Fmt object and define the base
757 758 759 |
# File 'lib/nio/fmt.rb', line 757 def Fmt.base(b, uppercase=nil) Fmt.default.base(b, uppercase) end |
.convert(x, type, mode = :approx) ⇒ Object
Numerical conversion: converts the quantity x
to an object of class type
.
The third parameter is the kind of conversion:
:approx
-
Tries to find an approximate simpler value if possible for inexact numeric types. This is the default. This is slower in general and may take some seconds in some cases.
:exact
-
Performs a conversion as exact as possible.
The third parameter is true for approximate conversion (inexact values are simplified if possible) and false for conversions as exact as possible.
1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 |
# File 'lib/nio/fmt.rb', line 1213 def Fmt.convert(x, type, mode=:approx) fmt = mode==:approx ? CONV_FMT : CONV_FMT_STRICT # return x.prec(type) if !(x.is_a?(type)) # return type.nio_read(x.nio_write(fmt),fmt) x = x.nio_write_neutral(fmt) x = type.nio_read_neutral(x) end x end |
.default ⇒ Object
Returns the current default format.
1025 1026 1027 1028 1029 1030 1031 1032 |
# File 'lib/nio/fmt.rb', line 1025 def self.default d = self[:def] if block_given? d = d.dup yield d end d end |
.default=(fmt) ⇒ Object
Defines the current default format.
1034 1035 1036 |
# File 'lib/nio/fmt.rb', line 1034 def self.default=(fmt) self[:def] = fmt end |
.default_rounding_mode ⇒ Object
Rounding mode used when not specified otherwise
579 580 581 |
# File 'lib/nio/fmt.rb', line 579 def Fmt.default_rounding_mode @@default_rounding_mode end |
.default_rounding_mode=(m) ⇒ Object
The default rounding can be changed here; it starts with the value :even. See the rounding modes available in the description of method #mode().
584 585 586 587 |
# File 'lib/nio/fmt.rb', line 584 def Fmt.default_rounding_mode=(m) @@default_rounding_mode=m Fmt.default = Fmt.default.round(m) end |
.grouping(grp = [3], grp_sep = nil) ⇒ Object
This is a shortcut to return a new default Fmt object and define the grouping as with #grouping().
501 502 503 |
# File 'lib/nio/fmt.rb', line 501 def Fmt.grouping(grp=[3],grp_sep=nil) Fmt.default.grouping(grp,grp_sep) end |
.insignificant_digits(v = '#') ⇒ Object
This is a shortcut to return a new default Fmt object and define insignificant digits
651 652 653 |
# File 'lib/nio/fmt.rb', line 651 def Fmt.insignificant_digits(v='#') Fmt.default.insignificant_digits(v) end |
.mode(mode, ndig = nil, options = {}) ⇒ Object
This is a shortcut to return a new default Fmt object and define the formatting mode as with #mode()
569 570 571 |
# File 'lib/nio/fmt.rb', line 569 def Fmt.mode(mode,ndig=nil,={}) Fmt.default.mode(mode,ndig,) end |
.pad0s(w) ⇒ Object
This is a shortcut to create a new Fmt object and define numeric padding as with #pad0s()
743 744 745 |
# File 'lib/nio/fmt.rb', line 743 def Fmt.pad0s(w) Fmt.default.pad0s(w) end |
.prec(ndig, mode = nil, options = {}) ⇒ Object
This is a shortcut to return a new default Fmt object and define the formatting mode as with #prec()
574 575 576 |
# File 'lib/nio/fmt.rb', line 574 def Fmt.prec(ndig,mode=nil,={}) Fmt.default.prec(ndig,mode,) end |
.read(cls, txt) ⇒ Object
61 62 63 |
# File 'lib/nio/sugar.rb', line 61 def Fmt.read(cls,txt) cls.nio_read(txt) end |
.rep(*params) ⇒ Object
This is a shortcut to return a new default Fmt object and define the repeating decimals mode as with #rep()
706 707 708 |
# File 'lib/nio/fmt.rb', line 706 def Fmt.rep(*params) Fmt.default.rep(*params) end |
.sci_digits(v = -1)) ⇒ Object
This is a shortcut to return a new default Fmt object and define sci_digits
656 657 658 |
# File 'lib/nio/fmt.rb', line 656 def Fmt.sci_digits(v=-1) Fmt.default.sci_digits(v) end |
.sep(dec_sep, grp_sep = nil, grp = nil) ⇒ Object
This is a shortcut to return a new default Fmt object and define the separators as with #sep().
496 497 498 |
# File 'lib/nio/fmt.rb', line 496 def Fmt.sep(dec_sep,grp_sep=nil,grp=nil) Fmt.default.sep(dec_sep,grp_sep,grp) end |
.show_all_digits(v = true) ⇒ Object
This is a shortcut to return a new default Fmt object and define show_all_digits
641 642 643 |
# File 'lib/nio/fmt.rb', line 641 def Fmt.show_all_digits(v=true) Fmt.default.show_all_digits(v) end |
.show_plus(v = true) ⇒ Object
This is a shortcut to return a new default Fmt object and define show_plus
671 672 673 |
# File 'lib/nio/fmt.rb', line 671 def Fmt.show_plus(v=true) Fmt.default.show_plus(v) end |
.width(w, adj = nil, ch = nil) ⇒ Object
This is a shortcut to create a new Fmt object and define the width parameters as with #widht()
738 739 740 |
# File 'lib/nio/fmt.rb', line 738 def Fmt.width(w,adj=nil,ch=nil) Fmt.default.width(w,adj,ch) end |
.write(x) ⇒ Object
47 48 49 |
# File 'lib/nio/sugar.rb', line 47 def Fmt.write(x) x.nio_write end |
Instance Method Details
#<<(x) ⇒ Object
38 39 40 |
# File 'lib/nio/sugar.rb', line 38 def <<(x) x.nio_write(self) end |
#>>(cls_txt) ⇒ Object
50 51 52 53 |
# File 'lib/nio/sugar.rb', line 50 def >>(cls_txt) cls,txt = cls_txt cls.nio_read(txt,self) end |
#approx_mode(mode) ⇒ Object
This defines the approximate mode (:only_sig, :exact, :simplify) just like the last parameter of #mode()
603 604 605 |
# File 'lib/nio/fmt.rb', line 603 def approx_mode(mode) dup.approx_mode! mode end |
#approx_mode!(mode) ⇒ Object
This is the mutator version of #approx_mode().
607 608 609 |
# File 'lib/nio/fmt.rb', line 607 def approx_mode!(mode) set! :approx=>mode end |
#base(b, uppercase = nil) ⇒ Object
defines the numerical base; the second parameters forces the use of uppercase letters for bases greater than 10.
749 750 751 |
# File 'lib/nio/fmt.rb', line 749 def base(b, uppercase=nil) dup.base! b, uppercase end |
#base!(b, uppercase = nil) ⇒ Object
This is the mutator version of #base().
753 754 755 |
# File 'lib/nio/fmt.rb', line 753 def base!(b, uppercase=nil) set! :base_radix=>b, :base_uppercase=>uppercase end |
#get_all_digits? ⇒ Boolean
return the show_all_digits state
788 789 790 |
# File 'lib/nio/fmt.rb', line 788 def get_all_digits? # :nodoc: @all_digits end |
#get_approx ⇒ Object
returns the approximate mode
792 793 794 |
# File 'lib/nio/fmt.rb', line 792 def get_approx # :nodoc: @approx end |
#get_base ⇒ Object
returns the base
767 768 769 |
# File 'lib/nio/fmt.rb', line 767 def get_base # :nodoc: @base_radix end |
#get_base_digits(b = nil) ⇒ Object
returns the digit characters used for a base
771 772 773 |
# File 'lib/nio/fmt.rb', line 771 def get_base_digits(b=nil) # :nodoc: (b.nil? || b==@base_radix) ? @base_digits : DigitsDef.base(b,!@base_uppercase) end |
#get_base_uppercase? ⇒ Boolean
returns true if uppercase digits are used
775 776 777 |
# File 'lib/nio/fmt.rb', line 775 def get_base_uppercase? # :nodoc: @base_uppercase end |
#get_exp_char(base) ⇒ Object
returns the exponent char used with the specified base
761 762 763 764 |
# File 'lib/nio/fmt.rb', line 761 def get_exp_char(base) # :nodoc: base ||= @base_radix base<=10 ? 'E' : '^' end |
#get_mode ⇒ Object
returns the formatting mode
780 781 782 |
# File 'lib/nio/fmt.rb', line 780 def get_mode # :nodoc: @mode end |
#get_ndig ⇒ Object
returns the precision (number of digits)
784 785 786 |
# File 'lib/nio/fmt.rb', line 784 def get_ndig # :nodoc: @ndig end |
#get_round ⇒ Object
returns the rounding mode
797 798 799 |
# File 'lib/nio/fmt.rb', line 797 def get_round # :nodoc: @round end |
#grouping(grp = [3], grp_sep = nil) ⇒ Object
This defines the grouping of digits (which can also be defined in #sep()
486 487 488 |
# File 'lib/nio/fmt.rb', line 486 def grouping(grp=[3],grp_sep=nil) dup.grouping!(grp,grp_sep) end |
#grouping!(grp = [3], grp_sep = nil) ⇒ Object
This is the mutator version of #grouping().
490 491 492 |
# File 'lib/nio/fmt.rb', line 490 def grouping!(grp=[3],grp_sep=nil) set! :grp_sep=>grp_sep, :grp=>grp end |
#insignificant_digits(ch = '#') ⇒ Object
Defines a character to stand for insignificant digits when a specific number of digits has been requested greater than then number of significant digits (for approximate types).
613 614 615 |
# File 'lib/nio/fmt.rb', line 613 def insignificant_digits(ch='#') dup.insignificant_digits! ch end |
#insignificant_digits!(ch = '#') ⇒ Object
This is the mutator version of #insignificant_digits().
617 618 619 620 |
# File 'lib/nio/fmt.rb', line 617 def insignificant_digits!(ch='#') ch ||= '' set! :non_sig=>ch end |
#mode(mode, precision = nil, options = {}) ⇒ Object
Define the formatting mode. There are two fixed parameters:
-
mode
(only relevant for output):gen
-
(general) chooses automatically the shortes format
:fix
-
(fixed precision) is a simple format with a fixed number of digits after the point
:sig
-
(significance precision) is like :fix but using significant digits
:sci
-
(scientific) is the exponential form 1.234E2
-
precision
(number of digits or :exact, only used for output)exact
-
means that as many digits as necessary to unambiguosly define the value are used; this is the default.
Other paramters can be passed in a hash after precision
-
:round
rounding mode applied to conversions (this is relevant for both input and output). It must be one of::inf
-
rounds to nearest with ties toward infinite;
1.5 is rounded to 2, -1.5 to -2
:zero
-
rounds to nearest with ties toward zero;
1.5 is rounded to 1, -1.5 to 2
:even
-
rounds to the nearest with ties toward an even digit;
1.5 rounds to 2, 2.5 to 2
-
:approx
approximate mode:only_sig
-
(the default) treats the value as an approximation and only significant digits (those that cannot take an arbitrary value without changing the specified value) are shown.
:exact
-
the value is interpreted as exact, there’s no distinction between significant and insignificant digits.
:simplify
-
the value is simplified, if possible to a simpler (rational) value.
-
:show_all_digits
if true, this forces to show digits that would otherwise not be shown in the:gen
format: trailing zeros of exact types or non-signficative digits of inexact types. -
:nonsignficative_digits
assigns a character to display insignificant digits, # by default
548 549 550 |
# File 'lib/nio/fmt.rb', line 548 def mode(mode,precision=nil,={}) dup.mode!(mode,precision,) end |
#mode!(mode, precision = nil, options = {}) ⇒ Object
This is the mutator version of #mode().
552 553 554 |
# File 'lib/nio/fmt.rb', line 552 def mode!(mode,precision=nil,={}) set! .merge(:mode=>mode, :ndig=>precision) end |
#nio_read_formatted(txt) ⇒ Object
:nodoc:
979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 |
# File 'lib/nio/fmt.rb', line 979 def nio_read_formatted(txt) # :nodoc: txt = txt.dup num = nil base = nil base ||= get_base zero = get_base_digits(base).digit_char(0).chr txt.tr!(@non_sig,zero) # we don't simply remove it because it may be before the radix point exp = 0 x_char = get_exp_char(base) exp_i = txt.index(x_char) exp_i = txt.index(x_char.downcase) if exp_i===nil if exp_i!=nil exp = txt[exp_i+1...txt.length].to_i txt = txt[0...exp_i] end opt = getRepDecOpt(base) if @rep_in #raise InvalidFormat,"Invalid numerical base" if base!=10 rd = RepDec.new # get_base not necessary: setS sets it from options rd.setS txt, opt num = rd.to_NeutralNum(opt.digits) else # to do: use RepDec.parse; then build NeutralNum directly opt.set_delim '','' opt.set_suffix '' rd = RepDec.new # get_base not necessary: setS sets it from options rd.setS txt, opt num = rd.to_NeutralNum(opt.digits) end num.rounding = get_round num.dec_pos += exp return num end |
#nio_write_formatted(neutral) ⇒ Object
Method used internally to format a neutral numeral
802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 |
# File 'lib/nio/fmt.rb', line 802 def nio_write_formatted(neutral) # :nodoc: str = '' if neutral.special? str << neutral.sign case neutral.special when :inf str << @inf_txt when :nan str << @nan_txt end else zero = get_base_digits(neutral.base).digit_char(0).chr neutral = neutral.dup round! neutral if neutral.zero? str << neutral.sign if neutral.sign=='-' # show - if number was <0 before rounding str << zero if @ndig.kind_of?(Numeric) && @ndig>0 && @mode==:fix str << @dec_sep << zero*@ndig end else neutral.trimLeadZeros actual_mode = @mode trim_trail_zeros = !@all_digits # false integral_digits = @sci_format if integral_digits == :eng integral_digits = 1 while (neutral.dec_pos - integral_digits).modulo(3) != 0 integral_digits += 1 end elsif integral_digits==:all || integral_digits < 0 if neutral.inexact? && @non_sig!='' && @ndig.kind_of?(Numeric) integral_digits = @ndig else integral_digits = neutral.digits.length end end exp = neutral.dec_pos - integral_digits case actual_mode when :gen # general (automatic) # @ndig means significant digits actual_mode = :sig actual_mode = :sci if use_scientific?(neutral, exp) trim_trail_zeros = !@all_digits # true end case actual_mode when :fix, :sig #, :gen str << neutral.sign if @show_plus || neutral.sign!='+' if @show_base && @base_prefix b_prefix = @base_indicators[neutral.base] str << b_prefix if b_prefix end if @ndig==:exact neutral.sign = '+' str << neutral.to_RepDec.getS(@rep_n, getRepDecOpt(neutral.base)) else #zero = get_base_digits.digit_char(0).chr ns_digits = '' nd = neutral.digits.length if actual_mode==:fix nd -= neutral.dec_pos end if neutral.inexact? && @ndig>nd # assert no rep-dec. ns_digits = @non_sig*(@ndig-nd) end digits = neutral.digits + ns_digits if neutral.dec_pos<=0 str << zero+@dec_sep+zero*(-neutral.dec_pos) + digits elsif neutral.dec_pos >= digits.length str << group(digits + zero*(neutral.dec_pos-digits.length)) else str << group(digits[0...neutral.dec_pos]) + @dec_sep + digits[neutral.dec_pos..-1] end end #str = str.chomp(zero).chomp(@dec_sep) if trim_trail_zeros && str.include?(@dec_sep) if trim_trail_zeros && str.include?(@dec_sep) && str[-@rep_auto.size..-1]!=@rep_auto str.chop! while str[-1]==zero[0] str.chomp!(@dec_sep) #puts str end when :sci str << neutral.sign if @show_plus || neutral.sign!='+' if @show_base && @base_prefix b_prefix = @base_indicators[neutral.base] str << b_prefix if b_prefix end #zero = get_base_digits.digit_char(0).chr if @ndig==:exact neutral.sign = '+' neutral.dec_pos-=exp str << neutral.to_RepDec.getS(@rep_n, getRepDecOpt(neutral.base)) else ns_digits = '' nd = neutral.digits.length if actual_mode==:fix nd -= neutral.dec_pos end if neutral.inexact? && @ndig>nd # assert no rep-dec. ns_digits = @non_sig*(@ndig-nd) end digits = neutral.digits + ns_digits str << ((integral_digits<1) ? zero : digits[0...integral_digits]) str << @dec_sep str << digits[integral_digits...@ndig] pad_right =(@ndig+1-str.length) str << zero*pad_right if pad_right>0 && !neutral.inexact? # maybe we didn't have enought digits end #str = str.chomp(zero).chomp(@dec_sep) if trim_trail_zeros && str.include?(@dec_sep) if trim_trail_zeros && str.include?(@dec_sep) && str[-@rep_auto.size..-1]!=@rep_auto str.chop! while str[-1]==zero[0] str.chomp!(@dec_sep) #puts str end str << get_exp_char(neutral.base) str << exp.to_s end end end if @show_base && !@base_prefix b_prefix = @base_indicators[neutral.base] str << b_prefix if b_prefix end if @width>0 && @fill_char!='' l = @width - str.length if l>0 case @adjust when :internal sign = '' if str[0,1]=='+' || str[0,1]=='-' sign = str[0,1] str = str[1...str.length] end str = sign + @fill_char*l + str when :center str = @fill_char*(l/2) + str + @fill_char*(l-l/2) when :right str = @fill_char*l + str when :left str = str + @fill_char*l end end end return str end |
#pad0s(w) ⇒ Object
Defines the justification (as #width()) with the given width, internal mode and filling with zeros.
Note that if you also use grouping separators, the filling 0s will not be separated.
729 730 731 |
# File 'lib/nio/fmt.rb', line 729 def pad0s(w) dup.pad0s! w end |
#pad0s!(w) ⇒ Object
This is the mutator version of #pad0s().
733 734 735 |
# File 'lib/nio/fmt.rb', line 733 def pad0s!(w) width! w, :internal, '0' end |
#prec(precision, mode = nil, options = {}) ⇒ Object
Defines the formatting mode like #mode() but using a different order of the first two parameters parameters, which is useful to change the precision only. Refer to #mode().
559 560 561 |
# File 'lib/nio/fmt.rb', line 559 def prec(precision,mode=nil, ={}) dup.prec! precision, mode, end |
#prec!(precision, mode = :gen, options = {}) ⇒ Object
This is the mutator version of #prec().
563 564 565 |
# File 'lib/nio/fmt.rb', line 563 def prec!(precision,mode=:gen, ={}) set! .merge(:mode=>mode, :ndig=>precision) end |
#read(cls, txt) ⇒ Object
54 55 56 |
# File 'lib/nio/sugar.rb', line 54 def read(cls,txt) cls.nio_read(txt,self) end |
#rep(*params) ⇒ Object
Defines the handling and notation for repeating numerals. The parameters can be passed in order or in a hash:
:begin
-
is the beginning delimiter of repeating section (<)
:end
-
is the ending delimiter of repeating section (<)
:suffix
-
is the suffix used to indicate a implicit repeating decimal
:rep
-
if this parameter is greater than zero, on output the repeating section is repeated the indicated number of times followed by the suffix; otherwise the delimited notation is used.
:read
-
(true/false) determines if repeating decimals are recognized on input (true)
687 688 689 |
# File 'lib/nio/fmt.rb', line 687 def rep(*params) dup.rep!(*params) end |
#rep!(*params) ⇒ Object
This is the mutator version of #rep().
691 692 693 694 695 696 697 698 699 700 701 702 |
# File 'lib/nio/fmt.rb', line 691 def rep!(*params) params << {} if params.size==0 if params[0].kind_of?(Hash) params = params[0] else begch,endch,autoch,rep,read = *params params = {:begin=>begch,:end=>endch,:suffix=>autoch,:nreps=>rep,:read=>read} end set! params end |
#round!(neutral) ⇒ Object
round a neutral numeral according to the format options
973 974 975 |
# File 'lib/nio/fmt.rb', line 973 def round!(neutral) # :nodoc: neutral.round! @ndig, @mode, @round end |
#sci_digits(n = -1)) ⇒ Object
Defines the number of significan digits before the radix separator in scientific notation. A negative value will set all significant digits before the radix separator. The special value :eng
activates engineering mode, in which the exponents are multiples of 3.
For example:
0.1234.nio_write(Fmt.mode(:sci,4).sci_digits(0) -> 0.1234E0
0.1234.nio_write(Fmt.mode(:sci,4).sci_digits(3) -> 123.4E-3
0.1234.nio_write(Fmt.mode(:sci,4).sci_digits(-1) -> 1234.E-4
0.1234.nio_write(Fmt.mode(:sci,4).sci_digits(:eng) -> 123.4E-3
631 632 633 |
# File 'lib/nio/fmt.rb', line 631 def sci_digits(n=-1) dup.sci_digits! n end |
#sci_digits!(n = -1)) ⇒ Object
This is the mutator version of #sci_digits().
635 636 637 |
# File 'lib/nio/fmt.rb', line 635 def sci_digits!(n=-1) set! :sci_format=>n end |
#sep(dec_sep, grp_sep = nil, grp = nil) ⇒ Object
Defines the separators used in numerals. This is relevant to both input and output.
The first argument is the radix point separator (usually a point or a comma; by default it is a point.)
The second argument is the group separator.
Finally, the third argument is an array that defines the groups of digits to separate. By default it’s [], which means that no grouping will be produced on output (but the group separator defined will be ignored in input.) To produce the common thousands separation a value of [3] must be passed, which means that groups of 3 digits are used.
477 478 479 |
# File 'lib/nio/fmt.rb', line 477 def sep(dec_sep,grp_sep=nil,grp=nil) dup.sep!(dec_sep,grp_sep,grp) end |
#sep!(dec_sep, grp_sep = nil, grp = nil) ⇒ Object
This is the mutator version of #sep().
481 482 483 |
# File 'lib/nio/fmt.rb', line 481 def sep!(dec_sep,grp_sep=nil,grp=nil) set! :dec_sep=>dec_sep, :grp_sep=>grp_sep, :grp=>grp end |
#show_all_digits(ad = true) ⇒ Object
This controls the display of the digits that are not necessary to specify the value unambiguosly (e.g. trailing zeros).
The true (default) value forces the display of the requested number of digits and false will display only necessary digits.
594 595 596 |
# File 'lib/nio/fmt.rb', line 594 def show_all_digits(ad=true) dup.show_all_digits! ad end |
#show_all_digits!(ad = true) ⇒ Object
This is the mutator version of #show_all_digits().
598 599 600 |
# File 'lib/nio/fmt.rb', line 598 def show_all_digits!(ad=true) set! :all_digits=>ad end |
#show_plus(sp = true) ⇒ Object
Controls the display of the sign for positive numbers
661 662 663 |
# File 'lib/nio/fmt.rb', line 661 def show_plus(sp=true) dup.show_plus! sp end |
#show_plus!(sp = true) ⇒ Object
This is the mutator version of #show_plus().
665 666 667 |
# File 'lib/nio/fmt.rb', line 665 def show_plus!(sp=true) set! :show_plus=>sp end |
#width(w, adj = nil, ch = nil) ⇒ Object
Sets the justificaton width, mode and fill character
The mode accepts these values:
:right
-
(the default) justifies to the right (adds padding at the left)
:left
-
justifies to the left (adds padding to the right)
:internal
-
like :right, but the sign is kept to the left, outside the padding.
:center
-
centers the number in the field
717 718 719 |
# File 'lib/nio/fmt.rb', line 717 def width(w,adj=nil,ch=nil) dup.width! w,adj,ch end |
#width!(w, adj = nil, ch = nil) ⇒ Object
This is the mutator version of #width().
721 722 723 |
# File 'lib/nio/fmt.rb', line 721 def width!(w,adj=nil,ch=nil) set! :width=>w, :adjust=>adj, :fill_char=>ch end |
#write(x) ⇒ Object
41 42 43 |
# File 'lib/nio/sugar.rb', line 41 def write(x) x.nio_write(self) end |