Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Truncating a number

  1. Mar 21, 2009 #1
    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?????
     
  2. jcsd
  3. Mar 21, 2009 #2

    MATLABdude

    User Avatar
    Science Advisor

    HINT: What does taking the base 10 logarithm of a number (in MATLAB, log10) tell you?
     
  4. Mar 22, 2009 #3
    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.
     
  5. Mar 22, 2009 #4

    MATLABdude

    User Avatar
    Science Advisor

    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: Mar 22, 2009
  6. Mar 23, 2009 #5
    Thanks mate was able to get it in the end sorry about the cross posting, novice user here.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Truncating a number
  1. Beast Numbers (Replies: 1)

  2. Number of digit (Replies: 2)

Loading...