Automatic generation produced by ISE Eiffel

Classes Clusters Cluster hierarchy Chart Relations Text Flat Contracts Flat contracts Go to:
indexing description: "Formatter for non-integral numbers" status: "See notice at end of class" names: format_double date: "$Date: 2001-11-16 20:32:23 +0000 (Fri, 16 Nov 2001) $" revision: "$Revision: 51435 $" class FORMAT_DOUBLE create make feature -- Initialization make (w, d: INTEGER) is require reasonable_field: w >= 1 reasonable_decimals: d <= w do set_defaults (w) decimals := d decimal := '.' ensure blank_fill: fill_character = ' ' show_sign_negative: show_sign_negative no_separator: no_separator width_set: width = w right_justified: right_justified leading_sign: leading_sign decimals_set: decimals = d decimal_point: decimal = '.' end set_defaults (w: INTEGER) is -- (from FORMAT_INTEGER) require -- from FORMAT_INTEGER reasonable_field: w >= 1 do blank_fill width := w sign_normal sign_negative_only right_justify ensure -- from FORMAT_INTEGER blank_fill: fill_character = ' ' show_sign_negative: show_sign_negative no_separator: no_separator width_set: width = w right_justified: right_justified leading_sign: leading_sign end feature -- Access after_decimal_separate: BOOLEAN -- Use separators after the decimal? bracketted_negative: BOOLEAN -- Enclose negative numbers in brackets? -- (from FORMAT_INTEGER) decimal: CHARACTER -- What is used for the decimal decimals: INTEGER -- Number of digits after the decimal point. fill_character: CHARACTER -- Padding character. -- (from FORMAT_INTEGER) justification: INTEGER -- Where in the field the format goes. -- (from FORMAT_INTEGER) separator: CHARACTER -- Separator between 1000's of numbers. -- (from FORMAT_INTEGER) sign_format: INTEGER -- How the sign is formatted. -- (from FORMAT_INTEGER) sign_string: STRING -- Formatting details for the sign. -- (from FORMAT_INTEGER) trailing_sign: BOOLEAN -- Is the sign at the end? -- (from FORMAT_INTEGER) width: INTEGER -- Width of the field. -- (from FORMAT_INTEGER) zero_not_shown: BOOLEAN -- Show 0.5 as .5 or 0.5? feature {NONE} -- Access arc_cosine (v: DOUBLE): DOUBLE is -- Trigonometric arccosine of radian v -- in the range [0, pi] -- (from DOUBLE_MATH) external "C | <math.h>" alias "acos" end arc_sine (v: DOUBLE): DOUBLE is -- Trigonometric arcsine of radian v -- in the range [-pi/2, +pi/2] -- (from DOUBLE_MATH) external "C | <math.h>" alias "asin" end arc_tangent (v: DOUBLE): DOUBLE is -- Trigonometric arctangent of radian v -- in the range [-pi/2, +pi/2] -- (from DOUBLE_MATH) external "C | <math.h>" alias "atan" end ceiling (v: DOUBLE): DOUBLE is -- Least integral greater than or equal to v -- (from DOUBLE_MATH) external "C | <math.h>" alias "ceil" end cosine (v: DOUBLE): DOUBLE is -- Trigonometric cosine of radian v approximated -- in the range [-pi/4, +pi/4] -- (from DOUBLE_MATH) external "C | <math.h>" alias "cos" end dabs (v: DOUBLE): DOUBLE is -- Absolute of v -- (from DOUBLE_MATH) external "C | <math.h>" alias "fabs" end Euler: DOUBLE is 2.7182818284590452354 -- Logarithm base -- (from MATH_CONST) exp (x: DOUBLE): DOUBLE is -- Exponential of v. -- (from DOUBLE_MATH) external "C | <math.h>" end floor (v: DOUBLE): DOUBLE is -- Greatest integral less than or equal to v -- (from DOUBLE_MATH) external "C | <math.h>" end log (v: DOUBLE): DOUBLE is -- Natural logarithm of v -- (from DOUBLE_MATH) external "C | <math.h>" end log10 (v: DOUBLE): DOUBLE is -- Base 10 logarithm of v -- (from DOUBLE_MATH) external "C | <math.h>" end log_2 (v: DOUBLE): DOUBLE is -- Base 2 logarithm of v -- (from DOUBLE_MATH) local a: DOUBLE do a := 2.0 Result := log (v) / log (a) end Pi: DOUBLE is 3.14159265358979323846 -- (from MATH_CONST) sine (v: DOUBLE): DOUBLE is -- Trigonometric sine of radian v approximated -- in range [-pi/4, +pi/4] -- (from DOUBLE_MATH) external "C | <math.h>" alias "sin" end sqrt (v: DOUBLE): DOUBLE is -- Square root of v -- (from DOUBLE_MATH) external "C | <math.h>" end Sqrt2: DOUBLE is 1.41421356237309504880 -- Square root of 2 -- (from MATH_CONST) tangent (v: DOUBLE): DOUBLE is -- Trigonometric tangent of radian v approximated -- in range [-pi/4, +pi/4] -- (from DOUBLE_MATH) external "C | <math.h>" alias "tan" end feature -- Status report centered: BOOLEAN is -- Are numbers to be formatted centered? -- (from FORMAT_INTEGER) do Result := justification = center_justification ensure -- from FORMAT_INTEGER Result = (justification = center_justification) end ignore_sign: BOOLEAN is -- Ignore the sign of a number? -- (from FORMAT_INTEGER) do Result := sign_format = ignore_sign_value ensure -- from FORMAT_INTEGER Result = (sign_format = ignore_sign_value) end leading_sign: BOOLEAN is -- Is the sign leading? -- (from FORMAT_INTEGER) do Result := not trailing_sign ensure -- from FORMAT_INTEGER Result = not trailing_sign end left_justified: BOOLEAN is -- Are numbers to be formatted with spaces on the right? -- (from FORMAT_INTEGER) do Result := justification = left_justification ensure -- from FORMAT_INTEGER Result = (justification = left_justification) end no_separator: BOOLEAN is -- Is there a separator? -- (from FORMAT_INTEGER) do Result := separator = '%U' ensure -- from FORMAT_INTEGER Result = (separator = '%U') end not_justified: BOOLEAN is -- Are numbers to be formatted in smallest string possible -- (from FORMAT_INTEGER) do Result := justification = no_justification ensure -- from FORMAT_INTEGER Result = (justification = no_justification) end right_justified: BOOLEAN is -- Are numbers to be formatted with spaces on the left? -- (from FORMAT_INTEGER) do Result := justification = right_justification ensure -- from FORMAT_INTEGER Result = (justification = right_justification) end show_sign: BOOLEAN is -- Are numbers to show sign whether positive or negative? -- (from FORMAT_INTEGER) do Result := sign_format = show_sign_value ensure -- from FORMAT_INTEGER Result = (sign_format = show_sign_value) end show_sign_negative: BOOLEAN is -- Are numbers to show sign only when negative? -- (from FORMAT_INTEGER) do Result := sign_format = sign_negative_value ensure -- from FORMAT_INTEGER Result = (sign_format = sign_negative_value) end show_sign_positive: BOOLEAN is -- Are numbers to show sign only when positive? -- (from FORMAT_INTEGER) do Result := sign_format = sign_positive_value ensure -- from FORMAT_INTEGER Result = (sign_format = sign_positive_value) end feature -- Status setting asterisk_fill is -- Fill numbers with asterisks. -- (from FORMAT_INTEGER) do fill_character := '*' ensure -- from FORMAT_INTEGER fill_character = '*' end blank_fill is -- Fill numbers with blanks. -- (from FORMAT_INTEGER) do fill_character := ' ' ensure -- from FORMAT_INTEGER fill_character = ' ' end bracket_negative is -- Bracket negative numbers. -- (from FORMAT_INTEGER) do bracketted_negative := True ensure -- from FORMAT_INTEGER bracketted_negative end center_justify is -- Put padding on right and left -- (from FORMAT_INTEGER) do justification := center_justification ensure -- from FORMAT_INTEGER centered end comma_decimal is -- Use , as the decimal point. do decimal := ',' ensure decimal = ',' end comma_separate is -- Set the separator to be comma. do separator := ',' separate_after_decimal ensure -- from FORMAT_INTEGER separator = ',' ensure then after_decimal_separate end dollar_fill is -- Fill numbers with dollars. -- (from FORMAT_INTEGER) do fill_character := '$' ensure -- from FORMAT_INTEGER fill_character = '$' end dot_separate is -- Set separator to period. -- (from FORMAT_INTEGER) do separator := '.' ensure -- from FORMAT_INTEGER separator = '.' end hide_zero is -- Show 0.5 as .5 . do zero_not_shown := True ensure zero_not_shown end left_justify is --Put padding on right -- (from FORMAT_INTEGER) do justification := left_justification ensure -- from FORMAT_INTEGER left_justified end no_justify is -- Always return the smallest string possible -- (from FORMAT_INTEGER) do justification := no_justification ensure -- from FORMAT_INTEGER not_justified end no_separate_after_decimal is -- Do not use separators after the decimal. do after_decimal_separate := False ensure not after_decimal_separate end point_decimal is -- Use . as the decimal point. do decimal := '.' ensure decimal = '.' end remove_separator is -- Remove the separator. do separator := '%U' no_separate_after_decimal ensure -- from FORMAT_INTEGER separator = '%U' ensure then not after_decimal_separate end right_justify is -- Put padding on left -- (from FORMAT_INTEGER) do justification := right_justification ensure -- from FORMAT_INTEGER right_justified end separate_after_decimal is -- Use separators after the decimal. do after_decimal_separate := True ensure after_decimal_separate end set_decimals (d: INTEGER) is -- d decimals to be displayed. require d <= width do decimals := d ensure decimals = d end set_fill (c: CHARACTER) is -- Fill numbers with c -- (from FORMAT_INTEGER) do fill_character := c ensure -- from FORMAT_INTEGER fill_character = c end set_separator (c: CHARACTER) is -- Set the separator to c -- (from FORMAT_INTEGER) do separator := c ensure -- from FORMAT_INTEGER separator = c end set_sign (s: STRING) is -- Set sign values for positive, zero, negative -- All values must be the same length. -- Stored as negative, zero, positive. -- (from FORMAT_INTEGER) require -- from FORMAT_INTEGER s /= void s.count >= 3 s.count \\ 3 = 0 do sign_string := clone (s) ensure -- from FORMAT_INTEGER sign_string.is_equal (s) end set_width (w: INTEGER) is -- Set width to w -- (from FORMAT_INTEGER) require -- from FORMAT_INTEGER wide_enough: w >= 1 do width := w ensure -- from FORMAT_INTEGER width = w end show_zero is -- Show 0.5 as 0.5 . do zero_not_shown := False ensure not zero_not_shown end sign_cr_dr is -- Set sign for CR/DR -- (from FORMAT_INTEGER) do sign_string := "CR DR" ensure -- from FORMAT_INTEGER sign_string.is_equal ("CR DR") end sign_dr_cr is -- Set sign for DR/CR -- (from FORMAT_INTEGER) do sign_string := "DR CR" ensure -- from FORMAT_INTEGER sign_string.is_equal ("DR CR") end sign_floating_dollar is -- Set sign for floating dollar. -- (from FORMAT_INTEGER) do sign_string := "$$$" sign_leading ensure -- from FORMAT_INTEGER sign_string.is_equal ("$$$") end sign_floating_dollar_signed is -- Set sign for floating dollar include sign. -- (from FORMAT_INTEGER) do sign_string := "-$ $+$" sign_leading ensure -- from FORMAT_INTEGER sign_string.is_equal ("-$ $+$") end sign_ignore is -- Do not show sign. -- (from FORMAT_INTEGER) do sign_format := ignore_sign_value ensure -- from FORMAT_INTEGER ignore_sign end sign_leading is -- Set the sign to lead -- (from FORMAT_INTEGER) do trailing_sign := False ensure -- from FORMAT_INTEGER leading_sign end sign_negative_only is -- Show sign for negative numbers only. -- (from FORMAT_INTEGER) do sign_format := sign_negative_value ensure -- from FORMAT_INTEGER show_sign_negative end sign_normal is -- Set sign for - and +. -- (from FORMAT_INTEGER) do sign_string := "- +" ensure -- from FORMAT_INTEGER sign_string.is_equal ("- +") end sign_positive_only is -- Show sign for positive numbers only. -- (from FORMAT_INTEGER) do sign_format := sign_positive_value ensure -- from FORMAT_INTEGER show_sign_positive end sign_show is -- Show sign for all numbers. -- (from FORMAT_INTEGER) do sign_format := show_sign_value ensure -- from FORMAT_INTEGER show_sign end sign_trailing is -- Set the sign to trail -- (from FORMAT_INTEGER) do trailing_sign := True ensure -- from FORMAT_INTEGER trailing_sign end unbracket_negative is -- Do not bracket negative numbers. -- (from FORMAT_INTEGER) do bracketted_negative := False ensure -- from FORMAT_INTEGER not bracketted_negative end underscore_separate is -- Set the separator to be underscore. do separator := '_' separate_after_decimal ensure -- from FORMAT_INTEGER separator = '_' ensure then after_decimal_separate end zero_fill is -- Fill numbers with zeros. -- (from FORMAT_INTEGER) do fill_character := '0' ensure -- from FORMAT_INTEGER fill_character = '0' end feature -- Conversion formatted (d: DOUBLE): STRING is -- Format d. local sign: INTEGER integral, fraction: DOUBLE ints, fracs: STRING value: DOUBLE do value := d sign := 1 if d < 0 then sign := - 1 value := - value end value := value + 5 * 10 ^ (- decimals - 1) integral := floor (value) fraction := floor ((value - integral) * 10 ^ (decimals + 1)) if not no_separator then ints := split_integral (integral.out) if after_decimal_separate then fracs := separate_fraction (pad_fraction (fraction)) else fracs := pad_fraction (fraction) end else ints := clone (integral.out) fracs := pad_fraction (fraction) end create Result.make (width) if integral /= 0 or else not zero_not_shown then Result.append (ints) end if not Result.has ('e') then Result.extend (decimal) if decimals > 0 then Result.append (fracs) end if not ignore_sign then Result := process_sign (Result, sign) end end if justification /= no_justification and then Result.count < width then Result := justify (Result) end ensure exists: Result /= void correct_length: not_justified or Result.count >= width end feature {NONE} -- Conversion fm_formatted (i: INTEGER): STRING is -- Format the integer. -- (from FORMAT_INTEGER) local sign: INTEGER unsigned: INTEGER do if i < 0 then sign := - 1 unsigned := - i elseif i > 0 then sign := 1; unsigned := i end if not no_separator then Result := split_integral (unsigned.out) else Result := clone (unsigned.out) end if not ignore_sign or bracketted_negative then Result := process_sign (Result, sign) end if justification /= no_justification and then Result.count < width then Result := justify (Result) end ensure -- from FORMAT_INTEGER exists: Result /= void correct_length: not_justified or Result.count >= width end feature {NONE} -- Implementation Center_justification: INTEGER is 2 -- (from FORMAT_INTEGER) Ignore_sign_value: INTEGER is 1 -- (from FORMAT_INTEGER) justify (s: STRING): STRING is -- Justify s. -- (from FORMAT_INTEGER) require -- from FORMAT_INTEGER room: s.count < width justification: justification /= no_justification local l, r: STRING i, t: INTEGER do Result := s if not centered then create l.make (width - s.count) from i := 1 until i > l.capacity loop l.extend (fill_character) i := i + 1 end if left_justified then Result.append (l) else Result.prepend (l) end else t := (width - s.count) // 2 if 2 * t + s.count < width then create l.make (t + 1) else create l.make (t) end create r.make (t) from i := 1 until i > r.capacity loop l.extend (fill_character) r.extend (fill_character) i := i + 1 end if i = l.capacity then l.extend (fill_character) end Result.prepend (l) Result.append (r) end end Left_justification: INTEGER is 1 -- (from FORMAT_INTEGER) No_justification: INTEGER is 0 -- (from FORMAT_INTEGER) pad_fraction (f: DOUBLE): STRING is -- Stretch or shrink f to length decimals . do Result := f.out Result.head (Result.count - 1) if Result.count > decimals then Result := Result.substring (1, decimals) else from until Result.count = decimals loop Result.precede ('0') end end ensure Result.count = decimals end process_sign (s: STRING; sn: INTEGER): STRING is -- Process sign related values. -- (from FORMAT_INTEGER) local sstring: STRING do Result := s if bracketted_negative and sn = - 1 then Result.precede ('(') Result.extend (')') end if ((show_sign_negative or show_sign) and sn = - 1) or else ((show_sign_positive or show_sign) and sn = 1) or else (show_sign and sn = 0) then sstring := sign_value (sn) end if sstring /= void then if trailing_sign then Result.append (sstring) else Result.prepend (sstring) end end end Right_justification: INTEGER is 3 -- (from FORMAT_INTEGER) separate_fraction (s: STRING): STRING is -- Apply separators to the fraction. require efficiency: separator /= '%U' local count, sep_length: INTEGER do from count := 1 create Result.make (width) until count > s.count - 3 loop from sep_length := 0 until sep_length = 3 loop Result.extend (s.item (count)) count := count + 1 sep_length := sep_length + 1 end Result.extend (separator) end from until count > s.count loop Result.extend (s.item (count)) count := count + 1 end end Show_sign_value: INTEGER is 0 -- (from FORMAT_INTEGER) Sign_negative_value: INTEGER is 3 -- (from FORMAT_INTEGER) Sign_positive_value: INTEGER is 2 -- (from FORMAT_INTEGER) sign_size: INTEGER is -- Size of the sign. -- (from FORMAT_INTEGER) do Result := sign_string.count // 3 ensure -- from FORMAT_INTEGER Result * 3 = sign_string.count end sign_value (i: INTEGER): STRING is -- Value of the sign. -- (from FORMAT_INTEGER) require -- from FORMAT_INTEGER correct_sign: - 1 <= i and i <= 1 local t: INTEGER do t := sign_size * (i + 1) + 1 Result := sign_string.substring (t, t + sign_size - 1) end split_integral (s: STRING): STRING is -- Apply separators to an integer -- (from FORMAT_INTEGER) require -- from FORMAT_INTEGER efficiency: separator /= '%U' local count, sep_length: INTEGER do from count := s.count create Result.make (width) until count <= 3 loop from sep_length := 0 until sep_length = 3 loop Result.precede (s.item (count)) count := count - 1 sep_length := sep_length + 1 end Result.precede (separator) end from until count = 0 loop Result.precede (s.item (count)) count := count - 1 end end invariant separate_all: no_separator implies not after_decimal_separate -- from ANY reflexive_equality: standard_is_equal (Current) reflexive_conformance: conforms_to (Current) -- from FORMAT_INTEGER sign_string_constraint: sign_string /= void wide_enough: width >= 1 no_justification <= justification and justification <= right_justification indexing library: "[ EiffelBase: Library of reusable components for Eiffel. ]" status: "[ Copyright 1986-2001 Interactive Software Engineering (ISE). For ISE customers the original versions are an ISE product covered by the ISE Eiffel license and support agreements. ]" license: "[ EiffelBase may now be used by anyone as FREE SOFTWARE to develop any product, public-domain or commercial, without payment to ISE, under the terms of the ISE Free Eiffel Library License (IFELL) at http://eiffel.com/products/base/license.html. ]" source: "[ Interactive Software Engineering Inc. ISE Building 360 Storke Road, Goleta, CA 93117 USA Telephone 805-685-1006, Fax 805-685-6869 Electronic mail <info@eiffel.com> Customer support http://support.eiffel.com ]" info: "[ For latest info see award-winning pages: http://eiffel.com ]" end -- class FORMAT_DOUBLE
Classes Clusters Cluster hierarchy Chart Relations Text Flat Contracts Flat contracts Go to:

-- Generated by ISE Eiffel --
For more details: www.eiffel.com