Formatting an array into currency

4 views (last 30 days)
Krutik Gamit
Krutik Gamit on 19 Oct 2021
Commented: dpb on 19 Oct 2021
I have an array called MonthlyIncomeLA the values it has are as follows [4.7948e+0.3, 4.9688e+0.3...so on] I used the
format bank
MonthlyInLa
it only showed the values in the command window, how do i get the whole array of MonthlyInLA to change?
  4 Comments
Krutik Gamit
Krutik Gamit on 19 Oct 2021
so there is no way I can change e+0.3 format to just integers?
dpb
dpb on 19 Oct 2021
Explain where/what you've trying to accomplish here...in the command window, no, there is no format defined that displays variables as integers unless the values are integral; then they will be shown as such (individually, if in an array that isn't fully integral, then the formatting for the array will be used).
>> format short % is four decimal digits
>> p=pi
p =
3.1416
>> format bank % is two digits
>> p
p =
3.14
>> p=round(p) % round to integer, but bank says two decimals
p =
3.00
>> format short % short will show is integer by dropping trailing zeros
>> p
p =
3
>> a=[pi 3] % but not if in an array with a non-integral value
a =
3.1416 3.0000
>>
You can, of course, DISPLAY values as wanted, but that doesn't change their internal representation.
>> fprintf('%3.0f %3d \n',a)
3 3
>>
NB: MATLAB has a peculiarity in that applying a '%d' formatting string to a non-integral value will result in using a '%e' formatting string. This is a safety net in MATLAB; in the C Standard it is undefined behavior (you lied to the compiler by not having a consistent variable type for the format).
So, the question is what behavior are you trying to achieve here?

Sign in to comment.

Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!