How to Truncate Numbers to Five Significant Digits in MATLAB?

  • Thread starter asset101
  • Start date
  • Tags
    Numbers
In summary, one approach to creating a Matlab function that converts an integer to an integer with only 5 significant digits is to use modulus and divide repeatedly to collect the digits of the integer in an array. Then, the array can be scanned and either truncated or rounded to get the desired output. Subforum cross-posting should also be avoided in the future.
  • #1
asset101
11
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
  • #2
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.
 
  • #3

What is truncating a number?

Truncating a number is the process of shortening a decimal number by removing digits after a certain point without rounding up or down.

Why do we truncate numbers?

We truncate numbers to simplify calculations and make them easier to work with. It also allows us to quickly estimate values without having to deal with excessive decimal places.

How do you truncate a number?

To truncate a number, you can simply drop the digits after the desired point. For example, if you want to truncate 3.14159 to two decimal places, you would drop the 159 and the number becomes 3.14.

What is the difference between truncating and rounding?

Truncating and rounding are both methods of shortening decimal numbers, but they differ in how they handle the digits after the desired point. Truncating simply drops these digits, while rounding either increases or decreases the last digit based on certain rules.

What are the limitations of truncating numbers?

Truncating numbers can lead to inaccurate results when dealing with very small or very large numbers, as important digits may be dropped. It also does not take into account the significance or precision of the numbers.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • Electrical Engineering
Replies
4
Views
839
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
7
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • General Math
Replies
4
Views
7K
  • General Math
4
Replies
125
Views
16K
  • Math Proof Training and Practice
2
Replies
67
Views
10K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
4K
Back
Top