Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Convert decimal hours into hh:mm:ss format

Former Member
0 Kudos

I want to convert decimals hours into hh:mm:ss

i.e. 1.38 into 1:22:48

please give me any function module or

is there any formula

1 ACCEPTED SOLUTION

former_member188685
Active Contributor

in simplerway you can do this..

REPORT  ztest_time_conv.

DATA: hour(2) TYPE n,
      min(2) TYPE n,
      sec(2) TYPE n,
      tim TYPE i.
DATA: time TYPE p DECIMALS 2.
DATA: timeformat TYPE sy-uzeit.
time = '1.38'.

tim = time * 3600.
hour = tim DIV 3600.
tim = tim MOD 3600.
min = tim DIV 60.
sec = tim MOD 60.


CONCATENATE hour min sec INTO timeformat.
WRITE timeformat.
BREAK-POINT.

5 REPLIES 5

Former Member
0 Kudos

Hi,

Please try this code..

DATA: v_value TYPE p DECIMALS 2.
  DATA: v_string TYPE string.
  DATA: v_int    TYPE numc2.
  DATA: v_time  TYPE syuzeit.

  v_value = '1.38'.

  IF v_value > '24.00' OR v_value < 0.
    MESSAGE s208(00) WITH 'Not possible'.
    EXIT.
  ENDIF.

* Move the value to a string.
  v_string = v_value.

* Hour.
  SPLIT v_string AT '.' INTO  v_time(2) v_int.


  CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
    EXPORTING
      input  = v_time(2)
    IMPORTING
      output = v_time(2).

* Minute
  v_value = 60 * v_int / 100.
  v_string = v_value.
  SPLIT v_string AT '.' INTO  v_time+2(2) v_int.

  CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
    EXPORTING
      input  = v_time+2(2)
    IMPORTING
      output = v_time+2(2).


* Second
  v_value = 60 * v_int / 100.
  v_string = v_value.
  SPLIT v_string AT '.' INTO v_time+4(2) v_int.

  CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
    EXPORTING
      input  = v_time+4(2)
    IMPORTING
      output = v_time+4(2).

  WRITE: / v_time.

Thanks

Naren

former_member188685
Active Contributor

in simplerway you can do this..

REPORT  ztest_time_conv.

DATA: hour(2) TYPE n,
      min(2) TYPE n,
      sec(2) TYPE n,
      tim TYPE i.
DATA: time TYPE p DECIMALS 2.
DATA: timeformat TYPE sy-uzeit.
time = '1.38'.

tim = time * 3600.
hour = tim DIV 3600.
tim = tim MOD 3600.
min = tim DIV 60.
sec = tim MOD 60.


CONCATENATE hour min sec INTO timeformat.
WRITE timeformat.
BREAK-POINT.

0 Kudos

Hi all

The example mentioned here leads to 0:19 Minutes in case of input of 0,33. I want to have it 0:20 Minutes (1/3 Hour). Therefore, I elaborated the following code:

*** Format hours minutes from a decimal representation which is passed in a text string
* e. g. 0,33 --> 0:20

  DATA: tim TYPE p DECIMALS 2,
        hour TYPE n,
        min_p TYPE p,
        min(2) TYPE n
e_time type char5,
i_time type char 5.


  REPLACE ',' IN i_time WITH '.'.
  tim = i_time.
  min_p = 60 / ( 100 / i_time ) * 100.
  min = min_p MOD 60.
  hour = min_p / 60.

  IF min = 59. " result 59 happens if we get 0,99 which is equal to one hour regarding the rounding difference
    min = '00'.
  ENDIF.
  if min > 29.  " in this case the hour calculation returns one hour too much due to rounding above
    hour = hour - 1.
  endif.

  WRITE min TO min ROUND 1 DECIMALS 0.

  CONCATENATE hour ':' min INTO e_time.

Edited by: Philippe Addor on Aug 26, 2010 1:50 PM

Edited by: Philippe Addor on Aug 26, 2010 1:52 PM

Former Member
0 Kudos

Hi Vijay,

Good one. I forgot about the MOD function ..

Regards

Naren

Former Member
0 Kudos

Duplicate..

Edited by: Narendran Muthukumaran on Sep 11, 2008 8:37 PM