PDA

View Full Version : Truncating a number


asset101
Mar21-09, 10:51 PM
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?????

MATLABdude
Mar21-09, 11:15 PM
HINT: What does taking the base 10 logarithm of a number (in MATLAB, log10) tell you?

asset101
Mar22-09, 12:11 AM
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.

MATLABdude
Mar22-09, 03:28 AM
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

asset101
Mar23-09, 04:45 PM
Thanks mate was able to get it in the end sorry about the cross posting, novice user here.