Formatting a double for output in message box

Hi,
in my program i have a message box, which informs the user that an activity has been terminated and also display the activities consumption. Something like this: 'The activity xxx has been terminated and the consumption was 4.925971e-01 kWh'
My problem: how do i get that (fairly) ugly and unreadable value '4.925971e-01' to the handy format of '0.492'?
Cheers, Mathias

 Accepted Answer

You want to use sprintf, probably something like this:
sprintf('%0.3f',val);
Or, more completely:
sprintf('The activity %s has been terminated and the consumption was %0.3f kWh', activity, val);

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!