MATLAB Truncating a Number to 5 Significant Digits in MATLAB

  • Thread starter Thread starter asset101
  • Start date Start date
AI Thread Summary
The discussion revolves around creating a MATLAB function that converts an integer to a format with only five significant digits. The initial logic proposed involves checking if the input integer exceeds 99999, as this is the threshold for truncation. If the integer is larger, additional logic is needed to format it correctly. A key insight shared is the use of the base 10 logarithm (log10) to determine the number of digits in the input number. By applying the formula `digits = floor(log10(number) + 1)`, users can calculate the digit count effectively. The conversation concludes with a user successfully implementing the solution after receiving guidance, highlighting the importance of understanding logarithmic functions in MATLAB for this task.
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?
 
Physics news on Phys.org
HINT: What does taking the base 10 logarithm of a number (in MATLAB, log10) tell you?
 
Taking the base 10 is to find out how many times you would have to multiply by 10 to get the number. As i understand it to be.
I am still a little trouble with getting the missing logic.
Can you push me just a little bit further.
 
asset101 said:
Taking the base 10 is to find out how many times you would have to multiply by 10 to get the number. As i understand it to be.
I am still a little trouble with getting the missing logic.
Can you push me just a little bit further.

Actually, you had the answer, you just phrased it in a manner which isn't so helpful ;-)

Taking the log10 of any number (greater than 1) gives you 1 less than the number of digits in that number.

Consider:
log10(10) = 1
log10(99) = 1.9956

So if you add 1 to log10(number > 1) and then rounded down, you'd get the number of digits in the number the user inputs.

>> digits = floor(log10(number) + 1)

And I think that's about as much as I can give you without giving away everything.

EDIT: Clarification
 
Last edited:
Thanks mate was able to get it in the end sorry about the cross posting, novice user here.
 
Back
Top