How to Truncate Numbers to Five Significant Digits in MATLAB?

  • Thread starter Thread starter asset101
  • Start date Start date
  • Tags Tags
    Numbers
Click For Summary
To truncate numbers to five significant digits in MATLAB, a function should be created that checks if the input integer exceeds 99999. If it does, logic must be implemented to truncate the number appropriately; otherwise, the number remains unchanged. One suggested method involves using modulus and division to extract digits into an array, which can then be processed to either truncate or round the integer. This approach resembles binary coded decimal arithmetic used in some computers. The discussion also emphasizes the importance of avoiding cross-posting in forums.
asset101
Messages
10
Reaction score
0
Write a Matlab Function file that takes one integer in and converts it to an integer with only 5 significant digits.
Eg Trunc(123)--------ans=123
Tunc(123345678) ----ans=12345e+03
----------------------------------------------------------------------------------------

My thoughts
First see that the max number that doesn't get truncated is 99999.

set up some logic
if n>99999
insert logic
else
n=n
(n is the value passed into the function).
Can anyone give us a hand?
Or suggest another approach
 
Physics news on Phys.org
One idea might be to use modulus and divide repeatedly to collect the digits of the integer in an array.

n = 12345678

then
a(0) = 8
a(1) = 7
...
a(7) = 1

Then scan down the array and either truncate or round the integer in the array. This is similar to the binary coded decimal arithmetic some computers have used.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 4 ·
Replies
4
Views
8K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 125 ·
5
Replies
125
Views
19K
  • · Replies 67 ·
3
Replies
67
Views
15K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K