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 or constructs a new format with the passed options.
-
.[]=(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(options = nil) ⇒ 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
- #[](options) ⇒ 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(options = nil) {|_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 = nil, 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(options = nil) {|_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 473 |
# File 'lib/nio/fmt.rb', line 431 def initialize(=nil) @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' set! if 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 or constructs a new format with the passed options.
1092 1093 1094 1095 1096 1097 1098 |
# File 'lib/nio/fmt.rb', line 1092 def self.[](tag) if tag.is_a?(Hash) Fmt(tag) else @@fmts[tag.to_sym] end end |
.[]=(tag, fmt_def) ⇒ Object
Assigns a format to a name in the formats repository.
1087 1088 1089 |
# File 'lib/nio/fmt.rb', line 1087 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
660 661 662 |
# File 'lib/nio/fmt.rb', line 660 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
789 790 791 |
# File 'lib/nio/fmt.rb', line 789 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.
1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 |
# File 'lib/nio/fmt.rb', line 1267 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(options = nil) ⇒ Object
Returns the current default format.
1073 1074 1075 1076 1077 1078 1079 1080 1081 |
# File 'lib/nio/fmt.rb', line 1073 def self.default(=nil) d = self[:def] d = d[] if if block_given? d = d.dup yield d end d end |
.default=(fmt) ⇒ Object
Defines the current default format.
1083 1084 1085 |
# File 'lib/nio/fmt.rb', line 1083 def self.default=(fmt) self[:def] = fmt end |
.default_rounding_mode ⇒ Object
Rounding mode used when not specified otherwise
593 594 595 |
# File 'lib/nio/fmt.rb', line 593 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().
598 599 600 601 |
# File 'lib/nio/fmt.rb', line 598 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().
513 514 515 |
# File 'lib/nio/fmt.rb', line 513 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
665 666 667 |
# File 'lib/nio/fmt.rb', line 665 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()
583 584 585 |
# File 'lib/nio/fmt.rb', line 583 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()
775 776 777 |
# File 'lib/nio/fmt.rb', line 775 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()
588 589 590 |
# File 'lib/nio/fmt.rb', line 588 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()
738 739 740 |
# File 'lib/nio/fmt.rb', line 738 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
670 671 672 |
# File 'lib/nio/fmt.rb', line 670 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().
508 509 510 |
# File 'lib/nio/fmt.rb', line 508 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
655 656 657 |
# File 'lib/nio/fmt.rb', line 655 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
703 704 705 |
# File 'lib/nio/fmt.rb', line 703 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
698 699 700 |
# File 'lib/nio/fmt.rb', line 698 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()
770 771 772 |
# File 'lib/nio/fmt.rb', line 770 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 |
#[](options) ⇒ Object
1064 1065 1066 |
# File 'lib/nio/fmt.rb', line 1064 def []() dup.set! end |
#approx_mode(mode) ⇒ Object
This defines the approximate mode (:only_sig, :exact, :simplify) just like the last parameter of #mode()
617 618 619 |
# File 'lib/nio/fmt.rb', line 617 def approx_mode(mode) dup.approx_mode! mode end |
#approx_mode!(mode) ⇒ Object
This is the mutator version of #approx_mode().
621 622 623 |
# File 'lib/nio/fmt.rb', line 621 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.
781 782 783 |
# File 'lib/nio/fmt.rb', line 781 def base(b, uppercase=nil) dup.base! b, uppercase end |
#base!(b, uppercase = nil) ⇒ Object
This is the mutator version of #base().
785 786 787 |
# File 'lib/nio/fmt.rb', line 785 def base!(b, uppercase=nil) set! :base_radix=>b, :base_uppercase=>uppercase end |
#get_all_digits? ⇒ Boolean
return the show_all_digits state
820 821 822 |
# File 'lib/nio/fmt.rb', line 820 def get_all_digits? # :nodoc: @all_digits end |
#get_approx ⇒ Object
returns the approximate mode
824 825 826 |
# File 'lib/nio/fmt.rb', line 824 def get_approx # :nodoc: @approx end |
#get_base ⇒ Object
returns the base
799 800 801 |
# File 'lib/nio/fmt.rb', line 799 def get_base # :nodoc: @base_radix end |
#get_base_digits(b = nil) ⇒ Object
returns the digit characters used for a base
803 804 805 |
# File 'lib/nio/fmt.rb', line 803 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
807 808 809 |
# File 'lib/nio/fmt.rb', line 807 def get_base_uppercase? # :nodoc: @base_uppercase end |
#get_exp_char(base) ⇒ Object
returns the exponent char used with the specified base
793 794 795 796 |
# File 'lib/nio/fmt.rb', line 793 def get_exp_char(base) # :nodoc: base ||= @base_radix base<=10 ? 'E' : '^' end |
#get_mode ⇒ Object
returns the formatting mode
812 813 814 |
# File 'lib/nio/fmt.rb', line 812 def get_mode # :nodoc: @mode end |
#get_ndig ⇒ Object
returns the precision (number of digits)
816 817 818 |
# File 'lib/nio/fmt.rb', line 816 def get_ndig # :nodoc: @ndig end |
#get_round ⇒ Object
returns the rounding mode
829 830 831 |
# File 'lib/nio/fmt.rb', line 829 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()
498 499 500 |
# File 'lib/nio/fmt.rb', line 498 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().
502 503 504 |
# File 'lib/nio/fmt.rb', line 502 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).
627 628 629 |
# File 'lib/nio/fmt.rb', line 627 def insignificant_digits(ch='#') dup.insignificant_digits! ch end |
#insignificant_digits!(ch = '#') ⇒ Object
This is the mutator version of #insignificant_digits().
631 632 633 634 |
# File 'lib/nio/fmt.rb', line 631 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
(optional), 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
560 561 562 |
# File 'lib/nio/fmt.rb', line 560 def mode(mode, precision=nil, ={}) dup.mode!(mode,precision,) end |
#mode!(mode, precision = nil, options = {}) ⇒ Object
This is the mutator version of #mode().
564 565 566 567 |
# File 'lib/nio/fmt.rb', line 564 def mode!(mode, precision=nil, ={}) precision, = nil, precision if .empty? && precision.is_a?(Hash) set! .merge(:mode=>mode, :ndig=>precision) end |
#nio_read_formatted(txt) ⇒ Object
:nodoc:
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 1060 1061 1062 |
# File 'lib/nio/fmt.rb', line 1023 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
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 1012 1013 1014 |
# File 'lib/nio/fmt.rb', line 834 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.
761 762 763 |
# File 'lib/nio/fmt.rb', line 761 def pad0s(w) dup.pad0s! w end |
#pad0s!(w) ⇒ Object
This is the mutator version of #pad0s().
765 766 767 |
# File 'lib/nio/fmt.rb', line 765 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().
572 573 574 |
# File 'lib/nio/fmt.rb', line 572 def prec(precision, mode=nil, ={}) dup.prec! precision, mode, end |
#prec!(precision, mode = nil, options = {}) ⇒ Object
This is the mutator version of #prec().
576 577 578 579 |
# File 'lib/nio/fmt.rb', line 576 def prec!(precision, mode=nil, ={}) mode, = nil, mode if .empty? && mode.is_a?(Hash) 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)
719 720 721 |
# File 'lib/nio/fmt.rb', line 719 def rep(*params) dup.rep!(*params) end |
#rep!(*params) ⇒ Object
This is the mutator version of #rep().
723 724 725 726 727 728 729 730 731 732 733 734 |
# File 'lib/nio/fmt.rb', line 723 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
1017 1018 1019 |
# File 'lib/nio/fmt.rb', line 1017 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
645 646 647 |
# File 'lib/nio/fmt.rb', line 645 def sci_digits(n=-1) dup.sci_digits! n end |
#sci_digits!(n = -1)) ⇒ Object
This is the mutator version of #sci_digits().
649 650 651 |
# File 'lib/nio/fmt.rb', line 649 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.
489 490 491 |
# File 'lib/nio/fmt.rb', line 489 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().
493 494 495 |
# File 'lib/nio/fmt.rb', line 493 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.
608 609 610 |
# File 'lib/nio/fmt.rb', line 608 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().
612 613 614 |
# File 'lib/nio/fmt.rb', line 612 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
686 687 688 |
# File 'lib/nio/fmt.rb', line 686 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().
690 691 692 693 694 |
# File 'lib/nio/fmt.rb', line 690 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
675 676 677 |
# File 'lib/nio/fmt.rb', line 675 def show_plus(sp=true) dup.show_plus! sp end |
#show_plus!(sp = true) ⇒ Object
This is the mutator version of #show_plus().
679 680 681 682 683 |
# File 'lib/nio/fmt.rb', line 679 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
749 750 751 |
# File 'lib/nio/fmt.rb', line 749 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().
753 754 755 |
# File 'lib/nio/fmt.rb', line 753 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 |