HP Forums

Full Version: How to round an HMS number? [SOLVED]
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'd like to know if it is posible to round the seconds in a number written in HMS format.

Here is an example. I'd like to round the number 15º16'16.99968" to 15º16'17". The function ROUND produces the following results:

ROUND(15º16'16.99968", 0) -> 15º00'00"
ROUND(15º16'16.99968", 1) -> 15º18'00"
ROUND(15º16'16.99968", 2) -> 15º16'12"
ROUND(15º16'16.99968", 3) -> 15º16'15.6"
ROUND(15º16'16.99968", 4) -> 15º16'17.04"

It seams that ROUND first converts 15º16'16.99968" to its decimal equivalent 15.2713888, then rounds this value to the specified number of decimal places (for example for 3 decimal places -> 15.271) and then converts this value back to the HMS format (15º16'15.6")

So, does anyone know how to get 15º16'17"?
I don't know it this could help you, but I do as above in my program Effemeridi (Ephemeris) with angles:

Code:

trunc_angle(ang)
// trunc decimal from a DEG form angle
BEGIN
  →HMS((IP(HMS→(ang)*3600)/3600));
END;

simp_angle(ang)
BEGIN
  LOCAL angle;
  angle:=360*FP(ang/360);
  IF angle<0 THEN
  angle:=angle+360
  END;
  RETURN angle;
END;

HNY!

Salvo
(01-01-2018 05:01 PM)salvomic Wrote: [ -> ]I don't know it this could help you, but I do as above in my program Effemeridi (Ephemeris) with angles:

Code:

trunc_angle(ang)
// trunc decimal from a DEG form angle
BEGIN
  →HMS((IP(HMS→(ang)*3600)/3600));
END;

simp_angle(ang)
BEGIN
  LOCAL angle;
  angle:=360*FP(ang/360);
  IF angle<0 THEN
  angle:=angle+360
  END;
  RETURN angle;
END;

HNY!

Salvo

Thanks Salvo! Your function applied to my case →HMS(IP(HMS→(15º16'16.99968")*3600)/3600) returns 15º16'16" - it truncates as its name suggests.

Inspired by your function I've found that →HMS(ROUND(HMS→(15º16'16.99968")*3600,0)/3600) returns 15º16'17" - the answer I was looking for.

HNY!
(01-01-2018 05:58 PM)JMB Wrote: [ -> ]Thanks Salvo! Your function applied to my case →HMS(IP(HMS→(15º16'16.99968")*3600)/3600) returns 15º16'16" - it truncates as its name suggests.

Inspired by your function I've found that →HMS(ROUND(HMS→(15º16'16.99968")*3600,0)/3600) returns 15º16'17" - the answer I was looking for.

HNY!

Happy! :-)

ciao
Hello,

Remember that unlike the 48 series of calculators, the HMS functions don't actually change the value underneath. Rather, they just set a flag to display/interpret the underlying value as a HMS input. This is why you can do things directly like SIN(<dms_number>), or + - * / and it will work directly without the need to do intermediate conversions like on the old system. It is much more convenient for every case EXCEPT the one you've found when you are trying to round a value like you are doing.
Thanks for your explanation, Tim.
With the new firmware version 2.0.0.13865 (2018 07 06), the behavior of ROUND (and also TRUNC) has been changed. Now using 0, 2 and 4 as the second argument, you can round to the nearest degree, minute or second.

The function ROUND produces now the following results:

ROUND(15º16'16.99968", 0) -> 15º00'00"
ROUND(15º16'16.99968", 2) -> 15º16'00"
ROUND(15º16'16.99968", 4) -> 15º16'17"
Also handles sig figs since a negative value into ROUND/TRUNC does total sigits, not after the dot.

Yes, this thread was the spur to get that changed.
Reference URL's