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_exp_plus(v = true) ⇒ Object
This is a shortcut to return a new default Fmt object and define show_exp_plus.
-
.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_exp_plus(sp = true) ⇒ Object
Controls the display of the sign for positive exponents.
-
#show_exp_plus!(sp = true) ⇒ Object
This is the mutator version of #show_plus().
-
#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.
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 462 463 464 465 466 467 468 469 470 471 472 |
# File 'lib/nio/fmt.rb', line 431 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 @show_exp_plus = false @plus_symbol = nil @minus_symbol = nil @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.
1083 1084 1085 |
# File 'lib/nio/fmt.rb', line 1083 def self.[](tag) @@fmts[tag.to_sym] end |
.[]=(tag, fmt_def) ⇒ Object
Assigns a format to a name in the formats repository.
1079 1080 1081 |
# File 'lib/nio/fmt.rb', line 1079 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
657 658 659 |
# File 'lib/nio/fmt.rb', line 657 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
786 787 788 |
# File 'lib/nio/fmt.rb', line 786 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.
1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 |
# File 'lib/nio/fmt.rb', line 1254 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.
1066 1067 1068 1069 1070 1071 1072 1073 |
# File 'lib/nio/fmt.rb', line 1066 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.
1075 1076 1077 |
# File 'lib/nio/fmt.rb', line 1075 def self.default=(fmt) self[:def] = fmt end |
.default_rounding_mode ⇒ Object
Rounding mode used when not specified otherwise
590 591 592 |
# File 'lib/nio/fmt.rb', line 590 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().
595 596 597 598 |
# File 'lib/nio/fmt.rb', line 595 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().
512 513 514 |
# File 'lib/nio/fmt.rb', line 512 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
662 663 664 |
# File 'lib/nio/fmt.rb', line 662 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()
580 581 582 |
# File 'lib/nio/fmt.rb', line 580 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()
772 773 774 |
# File 'lib/nio/fmt.rb', line 772 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()
585 586 587 |
# File 'lib/nio/fmt.rb', line 585 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()
735 736 737 |
# File 'lib/nio/fmt.rb', line 735 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
667 668 669 |
# File 'lib/nio/fmt.rb', line 667 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().
507 508 509 |
# File 'lib/nio/fmt.rb', line 507 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
652 653 654 |
# File 'lib/nio/fmt.rb', line 652 def Fmt.show_all_digits(v=true) Fmt.default.show_all_digits(v) end |
.show_exp_plus(v = true) ⇒ Object
This is a shortcut to return a new default Fmt object and define show_exp_plus
700 701 702 |
# File 'lib/nio/fmt.rb', line 700 def Fmt.show_exp_plus(v=true) Fmt.default.show_exp_plus(v) end |
.show_plus(v = true) ⇒ Object
This is a shortcut to return a new default Fmt object and define show_plus
695 696 697 |
# File 'lib/nio/fmt.rb', line 695 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()
767 768 769 |
# File 'lib/nio/fmt.rb', line 767 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()
614 615 616 |
# File 'lib/nio/fmt.rb', line 614 def approx_mode(mode) dup.approx_mode! mode end |
#approx_mode!(mode) ⇒ Object
This is the mutator version of #approx_mode().
618 619 620 |
# File 'lib/nio/fmt.rb', line 618 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.
778 779 780 |
# File 'lib/nio/fmt.rb', line 778 def base(b, uppercase=nil) dup.base! b, uppercase end |
#base!(b, uppercase = nil) ⇒ Object
This is the mutator version of #base().
782 783 784 |
# File 'lib/nio/fmt.rb', line 782 def base!(b, uppercase=nil) set! :base_radix=>b, :base_uppercase=>uppercase end |
#get_all_digits? ⇒ Boolean
return the show_all_digits state
817 818 819 |
# File 'lib/nio/fmt.rb', line 817 def get_all_digits? # :nodoc: @all_digits end |
#get_approx ⇒ Object
returns the approximate mode
821 822 823 |
# File 'lib/nio/fmt.rb', line 821 def get_approx # :nodoc: @approx end |
#get_base ⇒ Object
returns the base
796 797 798 |
# File 'lib/nio/fmt.rb', line 796 def get_base # :nodoc: @base_radix end |
#get_base_digits(b = nil) ⇒ Object
returns the digit characters used for a base
800 801 802 |
# File 'lib/nio/fmt.rb', line 800 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
804 805 806 |
# File 'lib/nio/fmt.rb', line 804 def get_base_uppercase? # :nodoc: @base_uppercase end |
#get_exp_char(base) ⇒ Object
returns the exponent char used with the specified base
790 791 792 793 |
# File 'lib/nio/fmt.rb', line 790 def get_exp_char(base) # :nodoc: base ||= @base_radix base<=10 ? 'E' : '^' end |
#get_mode ⇒ Object
returns the formatting mode
809 810 811 |
# File 'lib/nio/fmt.rb', line 809 def get_mode # :nodoc: @mode end |
#get_ndig ⇒ Object
returns the precision (number of digits)
813 814 815 |
# File 'lib/nio/fmt.rb', line 813 def get_ndig # :nodoc: @ndig end |
#get_round ⇒ Object
returns the rounding mode
826 827 828 |
# File 'lib/nio/fmt.rb', line 826 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()
497 498 499 |
# File 'lib/nio/fmt.rb', line 497 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().
501 502 503 |
# File 'lib/nio/fmt.rb', line 501 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).
624 625 626 |
# File 'lib/nio/fmt.rb', line 624 def insignificant_digits(ch='#') dup.insignificant_digits! ch end |
#insignificant_digits!(ch = '#') ⇒ Object
This is the mutator version of #insignificant_digits().
628 629 630 631 |
# File 'lib/nio/fmt.rb', line 628 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
559 560 561 |
# File 'lib/nio/fmt.rb', line 559 def mode(mode,precision=nil,={}) dup.mode!(mode,precision,) end |
#mode!(mode, precision = nil, options = {}) ⇒ Object
This is the mutator version of #mode().
563 564 565 |
# File 'lib/nio/fmt.rb', line 563 def mode!(mode,precision=nil,={}) set! .merge(:mode=>mode, :ndig=>precision) end |
#nio_read_formatted(txt) ⇒ Object
:nodoc:
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 |
# File 'lib/nio/fmt.rb', line 1020 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
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 971 972 973 974 975 976 977 978 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 |
# File 'lib/nio/fmt.rb', line 831 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 if @show_plus || neutral.sign!='+' str << ({'-'=>@minus_symbol, '+'=>@plus_symbol}[neutral.sign] || neutral.sign) end 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 if @show_plus || neutral.sign!='+' str << ({'-'=>@minus_symbol, '+'=>@plus_symbol}[neutral.sign] || neutral.sign) end 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) if @show_exp_plus || exp<0 str << (exp<0 ? (@minus_symbol || '-') : (@plus_symbol || '+')) end str << exp.abs.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.
758 759 760 |
# File 'lib/nio/fmt.rb', line 758 def pad0s(w) dup.pad0s! w end |
#pad0s!(w) ⇒ Object
This is the mutator version of #pad0s().
762 763 764 |
# File 'lib/nio/fmt.rb', line 762 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().
570 571 572 |
# File 'lib/nio/fmt.rb', line 570 def prec(precision,mode=nil, ={}) dup.prec! precision, mode, end |
#prec!(precision, mode = :gen, options = {}) ⇒ Object
This is the mutator version of #prec().
574 575 576 |
# File 'lib/nio/fmt.rb', line 574 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)
716 717 718 |
# File 'lib/nio/fmt.rb', line 716 def rep(*params) dup.rep!(*params) end |
#rep!(*params) ⇒ Object
This is the mutator version of #rep().
720 721 722 723 724 725 726 727 728 729 730 731 |
# File 'lib/nio/fmt.rb', line 720 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
1014 1015 1016 |
# File 'lib/nio/fmt.rb', line 1014 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
642 643 644 |
# File 'lib/nio/fmt.rb', line 642 def sci_digits(n=-1) dup.sci_digits! n end |
#sci_digits!(n = -1)) ⇒ Object
This is the mutator version of #sci_digits().
646 647 648 |
# File 'lib/nio/fmt.rb', line 646 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.
488 489 490 |
# File 'lib/nio/fmt.rb', line 488 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().
492 493 494 |
# File 'lib/nio/fmt.rb', line 492 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.
605 606 607 |
# File 'lib/nio/fmt.rb', line 605 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().
609 610 611 |
# File 'lib/nio/fmt.rb', line 609 def show_all_digits!(ad=true) set! :all_digits=>ad end |
#show_exp_plus(sp = true) ⇒ Object
Controls the display of the sign for positive exponents
683 684 685 |
# File 'lib/nio/fmt.rb', line 683 def show_exp_plus(sp=true) dup.show_exp_plus! sp end |
#show_exp_plus!(sp = true) ⇒ Object
This is the mutator version of #show_plus().
687 688 689 690 691 |
# File 'lib/nio/fmt.rb', line 687 def show_exp_plus!(sp=true) set! :show_exp_plus=>sp set! :plus_symbol=>sp if sp.kind_of?(String) self end |
#show_plus(sp = true) ⇒ Object
Controls the display of the sign for positive numbers
672 673 674 |
# File 'lib/nio/fmt.rb', line 672 def show_plus(sp=true) dup.show_plus! sp end |
#show_plus!(sp = true) ⇒ Object
This is the mutator version of #show_plus().
676 677 678 679 680 |
# File 'lib/nio/fmt.rb', line 676 def show_plus!(sp=true) set! :show_plus=>sp set! :plus_symbol=>sp if sp.kind_of?(String) self 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
746 747 748 |
# File 'lib/nio/fmt.rb', line 746 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().
750 751 752 |
# File 'lib/nio/fmt.rb', line 750 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 |