Formatting an array into currency
이전 댓글 표시
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
dpb
2021년 10월 19일
Formatting is only a property of the output display; the data values are still and always will be stored internally as double. Otherwise, you couldn't do any mathematical operations on them which would be most inconvenient for most cases.
Stephen23
2021년 10월 19일
"how do i get the whole array of MonthlyInLA to change?"
Neither integer nor floating point data types store any formatting information.
Formatting is purely an artifact of how those types are displayed.
Krutik Gamit
2021년 10월 19일
dpb
2021년 10월 19일
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?
답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!