Class: Nio::Fmt

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Methods included from StateEquivalent

#==, #===, #eql?, #hash

Constructor Details

#initialize {|_self| ... } ⇒ Fmt

Returns a new instance of Fmt.

Yields:

  • (_self)

Yield Parameters:

  • _self (Nio::Fmt)

    the object that the method was called on



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
462
463
464
465
# 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
  @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.



1076
1077
1078
# File 'lib/nio/fmt.rb', line 1076

def self.[](tag)
  @@fmts[tag.to_sym]
end

.[]=(tag, fmt_def) ⇒ Object

Assigns a format to a name in the formats repository.



1072
1073
1074
# File 'lib/nio/fmt.rb', line 1072

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



650
651
652
# File 'lib/nio/fmt.rb', line 650

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



779
780
781
# File 'lib/nio/fmt.rb', line 779

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.



1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
# File 'lib/nio/fmt.rb', line 1247

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

.defaultObject

Returns the current default format.



1059
1060
1061
1062
1063
1064
1065
1066
# File 'lib/nio/fmt.rb', line 1059

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.



1068
1069
1070
# File 'lib/nio/fmt.rb', line 1068

def self.default=(fmt)
  self[:def] = fmt
end

.default_rounding_modeObject

Rounding mode used when not specified otherwise



583
584
585
# File 'lib/nio/fmt.rb', line 583

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().



588
589
590
591
# File 'lib/nio/fmt.rb', line 588

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().



505
506
507
# File 'lib/nio/fmt.rb', line 505

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



655
656
657
# File 'lib/nio/fmt.rb', line 655

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()



573
574
575
# File 'lib/nio/fmt.rb', line 573

def Fmt.mode(mode,ndig=nil,options={})
  Fmt.default.mode(mode,ndig,options)
end

.pad0s(w) ⇒ Object

This is a shortcut to create a new Fmt object and define numeric padding as with #pad0s()



765
766
767
# File 'lib/nio/fmt.rb', line 765

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()



578
579
580
# File 'lib/nio/fmt.rb', line 578

def Fmt.prec(ndig,mode=nil,options={})
  Fmt.default.prec(ndig,mode,options)
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()



728
729
730
# File 'lib/nio/fmt.rb', line 728

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



660
661
662
# File 'lib/nio/fmt.rb', line 660

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().



500
501
502
# File 'lib/nio/fmt.rb', line 500

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



645
646
647
# File 'lib/nio/fmt.rb', line 645

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



693
694
695
# File 'lib/nio/fmt.rb', line 693

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



688
689
690
# File 'lib/nio/fmt.rb', line 688

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()



760
761
762
# File 'lib/nio/fmt.rb', line 760

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()



607
608
609
# File 'lib/nio/fmt.rb', line 607

def approx_mode(mode)
  dup.approx_mode! mode
end

#approx_mode!(mode) ⇒ Object

This is the mutator version of #approx_mode().



611
612
613
# File 'lib/nio/fmt.rb', line 611

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.



771
772
773
# File 'lib/nio/fmt.rb', line 771

def base(b, uppercase=nil)
  dup.base! b, uppercase
end

#base!(b, uppercase = nil) ⇒ Object

This is the mutator version of #base().



775
776
777
# File 'lib/nio/fmt.rb', line 775

def base!(b, uppercase=nil)
  set! :base_radix=>b, :base_uppercase=>uppercase
end

#get_all_digits?Boolean

return the show_all_digits state

Returns:

  • (Boolean)


810
811
812
# File 'lib/nio/fmt.rb', line 810

def get_all_digits? # :nodoc:
  @all_digits
end

#get_approxObject

returns the approximate mode



814
815
816
# File 'lib/nio/fmt.rb', line 814

def get_approx # :nodoc:
  @approx
end

#get_baseObject

returns the base



789
790
791
# File 'lib/nio/fmt.rb', line 789

def get_base # :nodoc:
  @base_radix
end

#get_base_digits(b = nil) ⇒ Object

returns the digit characters used for a base



793
794
795
# File 'lib/nio/fmt.rb', line 793

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

Returns:

  • (Boolean)


797
798
799
# File 'lib/nio/fmt.rb', line 797

def get_base_uppercase? # :nodoc:
  @base_uppercase
end

#get_exp_char(base) ⇒ Object

returns the exponent char used with the specified base



783
784
785
786
# File 'lib/nio/fmt.rb', line 783

def get_exp_char(base) # :nodoc:
  base ||= @base_radix
  base<=10 ? 'E' : '^'
end

#get_modeObject

returns the formatting mode



802
803
804
# File 'lib/nio/fmt.rb', line 802

def get_mode # :nodoc:
  @mode
end

#get_ndigObject

returns the precision (number of digits)



806
807
808
# File 'lib/nio/fmt.rb', line 806

def get_ndig # :nodoc:
  @ndig
end

#get_roundObject

returns the rounding mode



819
820
821
# File 'lib/nio/fmt.rb', line 819

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()



490
491
492
# File 'lib/nio/fmt.rb', line 490

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().



494
495
496
# File 'lib/nio/fmt.rb', line 494

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).



617
618
619
# File 'lib/nio/fmt.rb', line 617

def insignificant_digits(ch='#')
  dup.insignificant_digits! ch
end

#insignificant_digits!(ch = '#') ⇒ Object

This is the mutator version of #insignificant_digits().



621
622
623
624
# File 'lib/nio/fmt.rb', line 621

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



552
553
554
# File 'lib/nio/fmt.rb', line 552

def mode(mode,precision=nil,options={})
  dup.mode!(mode,precision,options)
end

#mode!(mode, precision = nil, options = {}) ⇒ Object

This is the mutator version of #mode().



556
557
558
# File 'lib/nio/fmt.rb', line 556

def mode!(mode,precision=nil,options={})
  set! options.merge(:mode=>mode, :ndig=>precision)
end

#nio_read_formatted(txt) ⇒ Object

:nodoc:



1013
1014
1015
1016
1017
1018
1019
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
# File 'lib/nio/fmt.rb', line 1013

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



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
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
# File 'lib/nio/fmt.rb', line 824

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.



751
752
753
# File 'lib/nio/fmt.rb', line 751

def pad0s(w)
  dup.pad0s! w
end

#pad0s!(w) ⇒ Object

This is the mutator version of #pad0s().



755
756
757
# File 'lib/nio/fmt.rb', line 755

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().



563
564
565
# File 'lib/nio/fmt.rb', line 563

def prec(precision,mode=nil, options={})
  dup.prec! precision, mode, options
end

#prec!(precision, mode = :gen, options = {}) ⇒ Object

This is the mutator version of #prec().



567
568
569
# File 'lib/nio/fmt.rb', line 567

def prec!(precision,mode=:gen, options={})
  set! options.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)



709
710
711
# File 'lib/nio/fmt.rb', line 709

def rep(*params)
  dup.rep!(*params)
end

#rep!(*params) ⇒ Object

This is the mutator version of #rep().



713
714
715
716
717
718
719
720
721
722
723
724
# File 'lib/nio/fmt.rb', line 713

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



1007
1008
1009
# File 'lib/nio/fmt.rb', line 1007

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


635
636
637
# File 'lib/nio/fmt.rb', line 635

def sci_digits(n=-1)
  dup.sci_digits! n
end

#sci_digits!(n = -1)) ⇒ Object

This is the mutator version of #sci_digits().



639
640
641
# File 'lib/nio/fmt.rb', line 639

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.



481
482
483
# File 'lib/nio/fmt.rb', line 481

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().



485
486
487
# File 'lib/nio/fmt.rb', line 485

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.



598
599
600
# File 'lib/nio/fmt.rb', line 598

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().



602
603
604
# File 'lib/nio/fmt.rb', line 602

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



676
677
678
# File 'lib/nio/fmt.rb', line 676

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().



680
681
682
683
684
# File 'lib/nio/fmt.rb', line 680

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



665
666
667
# File 'lib/nio/fmt.rb', line 665

def show_plus(sp=true)
  dup.show_plus! sp
end

#show_plus!(sp = true) ⇒ Object

This is the mutator version of #show_plus().



669
670
671
672
673
# File 'lib/nio/fmt.rb', line 669

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



739
740
741
# File 'lib/nio/fmt.rb', line 739

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().



743
744
745
# File 'lib/nio/fmt.rb', line 743

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