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
SUMMARY

The discussion focuses on creating a MATLAB function to truncate integers to five significant digits. The maximum integer that remains untruncated is 99999, while integers exceeding this threshold should be processed to yield results in scientific notation, such as converting 123345678 to 12345e+03. Participants suggest using logic to determine whether to truncate or round the integer, and propose utilizing modulus operations to extract digits into an array for further manipulation.

PREREQUISITES
  • Understanding of MATLAB function creation
  • Familiarity with integer data types in MATLAB
  • Knowledge of scientific notation representation
  • Basic array manipulation techniques in MATLAB
NEXT STEPS
  • Research MATLAB function syntax and best practices
  • Learn about MATLAB's array indexing and manipulation techniques
  • Explore methods for converting integers to scientific notation in MATLAB
  • Investigate algorithms for digit extraction and manipulation
USEFUL FOR

MATLAB programmers, data analysts, and anyone interested in numerical methods for truncating and formatting integers in MATLAB.

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
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 24 ·
Replies
24
Views
7K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 15 ·
Replies
15
Views
5K
  • · Replies 4 ·
Replies
4
Views
9K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 125 ·
5
Replies
125
Views
20K