Skip to content
Snippets Groups Projects
Commit 1dabc7a4 authored by Pieter Donker's avatar Pieter Donker
Browse files

added nof_digits_int and changed int_to_str

parent 00535ea6
Branches
No related tags found
No related merge requests found
......@@ -31,6 +31,7 @@ PACKAGE common_str_pkg IS
TYPE t_str_4_arr IS ARRAY (INTEGER RANGE <>) OF STRING(1 TO 4);
FUNCTION nof_digits(number: NATURAL) RETURN NATURAL;
FUNCTION nof_digits_int(number: INTEGER) RETURN NATURAL;
FUNCTION time_to_str(in_time : TIME) RETURN STRING;
FUNCTION str_to_time(in_str : STRING) RETURN TIME;
......@@ -70,6 +71,27 @@ PACKAGE BODY common_str_pkg IS
END IF;
END;
FUNCTION nof_digits_int(number: INTEGER) RETURN NATURAL IS
-- Returns number of digits in a natural number. Only used in string processing, so defined here.
-- log10(0) is not allowed so:
-- . nof_digits(0) = 1
-- We're adding 1 so:
-- . nof_digits(1) = 1
-- . nof_digits(9) = 1
-- . nof_digits(10) = 2
-- . nof_digits(1) = 2
BEGIN
IF number=0 THEN
RETURN 1;
ELSE
IF number > 0 THEN
RETURN floor_log10(number)+1;
ELSE
RETURN floor_log10(-1*number)+2;
END IF;
END IF;
END;
FUNCTION time_to_str(in_time : TIME) RETURN STRING IS
CONSTANT c_max_len_time : NATURAL := 20;
VARIABLE v_line : LINE;
......@@ -181,12 +203,12 @@ PACKAGE BODY common_str_pkg IS
FUNCTION int_to_str(int: INTEGER) RETURN STRING IS
-- CONSTANT c_max_len_int : NATURAL := 20;
VARIABLE v_line: LINE;
VARIABLE v_str: STRING(1 TO nof_digits(int)):= (OTHERS => ' ');
VARIABLE v_line: LINE;
VARIABLE v_str: STRING(1 TO nof_digits_int(int)):= (OTHERS => ' ');
BEGIN
STD.TEXTIO.WRITE(v_line, int);
v_str(v_line.ALL'RANGE) := v_line.ALL;
deallocate(v_line);
STD.TEXTIO.WRITE(v_line, int);
v_str(v_line.ALL'RANGE) := v_line.ALL;
deallocate(v_line);
RETURN v_str;
END;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment