division of float numbers

Hi, MATLAB is usually showed the result of float number with four digits, how we can get only two digits after the decimal point. Ex: a = 532.7589 I want to get it as a=532.76 and please I DONOT want to use fprintf('%.2f', a) Thanks in advance

Answers (1)

See the documentation for the format (link) function.
Specifically:
format bank
will do what you want.

9 Comments

@Fatina, note that you're in luck that there is such a format predefined (bank). There is no way to have a custom display different than the few predefined by format. So if you wanted 3 digits for example, you would have to use fprintf.
The issue with the bank format, it shows all the numbers with two decimal points even if the number is an integer number i.e if I have x=34 I will get it as x=34.00
Yes? You did not ask for anything different.
Question: what output would you want for 34.00389 ? How about for 34.00000000000000710542735760100185871124267578125 ?
@Walter — Thank you!
Are you answering my question OR you have such a question as what you have written in above!?
Your original question has already been answered: To output with two decimal places, use "format bank".
You seem to have a new question in which you want some numbers to be output with two decimal places, and other numbers to be output with no decimal places. I am asking you to clarify your needs: in this new question, what would be the desired output for 34.00389 and for 34.00000000000000710542735760100185871124267578125 ?
it is the same question, I have a set of different numbers, some are integers, and others are floats. if I set the format to bank in the command window, I will get all the numbers even the integers with two decimal points, however, I would like to get only two digits after the decimal point when I have float number, and the integer number should be displayed as it is, so s=4.548934 should be s=4.55 and n=89 should be n=89 NOT 89.00
is it clear?
If you want what you describe, you have to use the round function in addition to format short g:
format shortg
x = [4.548934 89.0009];
x = round(x,2)
x =
4.55 89
You have to live with the constraints of the software you use.
Now, 34.00000000000000710542735760100185871124267578125 is not an integer, but it rounds to an integer -- it is the very next representable number after 34 exactly. Should 34.00000000000000710542735760100185871124267578125 be displayed as 34 or as 34.00 ?

Sign in to comment.

Tags

Asked:

on 29 Oct 2017

Commented:

on 30 Oct 2017

Community Treasure Hunt

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

Start Hunting!