Better way to combine number with fraction?
Mostra commenti meno recenti
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 ?
Risposta accettata
Più risposte (1)
Jon
il 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 Commenti
Jon
il 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
il 2 Feb 2024
Categorie
Scopri di più su Characters and Strings in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!