Better way to combine number with fraction?
Show older comments
I have this struct output (from ROS2 message):
MessageType: 'builtin_interfaces/Time'
sec: 1706819594
nanosec: 685974901
I want to combine sec and nanosec in one value like this:
1706819594.685974901
I used the following line of code (and it worked):
zBase.header.stamp.sec+ sprintf(".%d",zBase.header.stamp.nanosec)
Is there a better way ?
Accepted Answer
More Answers (1)
Jon
on 1 Feb 2024
If you want to use it as a numerical value I would do this
val = str2double(zBase.header.stamp.sec) + str2double(zBase.header.stamp.nanosec)/1e9
4 Comments
Jon
on 1 Feb 2024
Even if you want to use it as a string later you could always convert the double back to a string using num2str
"Even if you want to use it as a string later you could always convert the double back to a string using num2str"
With the understanding that the interim DOUBLE will have lost information that cannot be retrieved:
fprintf('%.40f\n',1706819594.685974901)
Haisam Khaled
on 2 Feb 2024
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!