User's Tricks...


Time Conversion From Fractions

As One of Our Author Interested to Post this Time Conversion Function So that it would be Helpful. On Behalf of him (Mahesh) , I would like to post...

So Let's Get into Situation if you need a small tool like Converting Time Fraction from NAV standards Like 1.98 hours to 1 hour and 58 mins...

You might want to represent as 1:58 So to achieve that..

i would like to implement a CodeUnit  .Let's Declare a CodeUnit Time Calculator ..

Lets Create a Function Say ConvToHour and property of function Local to NO like Below...

Now Declare a parameter for Function as we would use fraction decimal in convertion...like below

select Function and Click Locals and define a Parameter of datatype Decimal...
Name               Datatype
TimeInDec    Decimal....



Next we want to return a text like 1:58  in my example to calling place. So  Set return Type to TEXT..



You can also return return as Time Datatype....

So now we should declare a Local String Variable which we wanted to send back...after Modifications Of Course..


NOW getting into business declare Globals (you may also use locals but i used globals formally)..

Name                DataType
hrs                          Integer
min                          Integer
secs                           Integer
frac_req_Min           Decimal
frac_req_secs         Decimal
Time1                 Time




Now write the Logic  for converting...

Not Using any Alien Tech functions Just Using a ROUND Function ,I hope you may came across..
For Begginers Sample: ROUND(11.9) = 11


OnRun()
MESSAGE(ConvToHours(1.98));

In Above I'm Calling the Function which Would return text so directly Displaying that in Message Function...


hrs := ROUND(TimeinDec,1,'<');

//IN Above statement we would get hours...

//Getting 0.98 and Converting 0.98 fraction into minutes i.e 0.98 * 60 = 58.80
frac_req_Min := TimeinDec - hrs;
min := ROUND(60 * frac_req_Min,1,'<');

//Getting 0.80 and Converting 0.80 fraction into minutes i.e 0.80 * 60 = 48 sec

frac_req_secs := (60 * frac_req_Min) - min;
secs := ROUND(60 * frac_req_secs,1,'<');

//Converting Integer to String as we cant directly send 1:58 isn't it

Str := FORMAT(hrs);

//converting Minutes to string 

IF min > 9 THEN
Str := Str + ':' + FORMAT(min)
ELSE
  Str := Str + ':0' + FORMAT(min);


//converting Secs to String

IF secs > 9 THEN
Str := Str + ':' + FORMAT(secs)
ELSE
  Str := Str + ':0' + FORMAT(secs);

//Just To Test
//You Could convert it to Time if needed 
EVALUATE(Time1,Str);
MESSAGE('%1',Time1);

//This is to return from where you calling....Hope you understand...
EXIT(Str);


Screen shot of Code...

OutPut


Let me Know if you have any Queries....
Thank You..

No comments:

Post a Comment