Stripping decimal places

Discussion in 'Questions (Windows Mobile)' started by Softselect, Nov 2, 2007.

  1. Softselect

    Softselect Member Licensed User

    Hi All,
    in my app I need to strip decimal places from a string after calculatio
    I need to strip to 3 places, but if the calculation only returns 2 then I get an error
    Can some one please help, its late and I have brain freeze, cant seem to figure it out
    Attached sample file
    Thanks
    Friedrich
     

    Attached Files:

  2. Cableguy

    Cableguy Expert Licensed User

    I think you are not aproaching the problem the best way...

    You are triming the string to find the decimal point and then fixing its length...

    I would go with the StrSplit aproach, as it takes a string of any length and split it into any number of strings with a comom separator..

    In this case, using "," as the separator we would get alway 2 strings(eg; 12,067 would result in a strig containing "12" and another containing "067")
    Then by checking the length of the second (decimal) string you could perform whatever task associated...

    Try this:
    PS. although I think the code is correct, i cannot trim more than 2 number from the end of the string, can't figure why?
     

    Attached Files:

    Last edited: Nov 2, 2007
  3. Scubaticus

    Scubaticus Active Member Licensed User

    Perhaps the Format command will help?

    Format("+123456","F3") -> 123456.000
    Format("+1.23456", "F3") -> 1.234

    Scub
     
    Last edited: Nov 2, 2007
  4. Rioven

    Rioven Active Member Licensed User

    Rounding or stripping

    Hi,

    For just stripping decimal places you may also apply this solution: [@scubaticus: I've learned new one, thanks]
    r="1E"&num1.Value
    newnumber=Int(textbox1.Text*r)/r

    For rounding decimal places:

    r="1E"&num1.Value
    newnumber=Int(textbox1.Text*r+.5)/r 'here you can manipulate the '.5' for rounding up or down.

    or simply use..

    newnumber=Round(textbox1.Text,num1.Value)


    Regards,
     
    Last edited: Nov 3, 2007
  5. Softselect

    Softselect Member Licensed User

    Solved

    Hi everyone,
    I have solved my problem of restricting decimal places to 3
    it is probebly not the best way but it works if you have less than 2 decimal places. (and i used a goto, big sin i know)
    The reason i need to do it is, that I need to store it to a file for later printing of report via PC and this needs to be done via WIFI and automaticaly
    on the PC i will use a utility that sits and look at a directory and print s any file that appears in it.

    of corse this all still needs to be done!!!
    just thought i might share it with yous
    Danke
    Friedrich
     
  6. klaus

    klaus Expert Licensed User

    Hello,
    I suggest you a solution in the joined file.
    The idea is to calculate the number of digits after the decimal point.
    Then
    if the number is less then 3 you add the missing zeros
    else you stripp it normaly.
    I hope this helps you even you found anouther solution.

    Klaus
    Switzerland
     

    Attached Files:

  7. Softselect

    Softselect Member Licensed User

    Thank you Klaus,
    That is what i was trying, but i am not actualy a programmer, so i tend to do tings the long way round.
    :sign0188:
    I will use it like u sugest.
    Danke
    Friedrich:)
     
  8. klaus

    klaus Expert Licensed User

    Hello Friedrich
    Do you want only to strip your data or do you want to round them before, in that case you can first round your numbers with the function
    New = Round(Old,3)
    and after that add the missing zeros if necessary as in the example I sent you.

    Klaus Switzerland
     
  9. Softselect

    Softselect Member Licensed User

    Sample Solution

    Hi Everyone,
    Just in case someone is interested, I have written a new routing to trim decimal places, add missing zeros and add a decimal point if none is passed.
    The routine is called with a variable for the number of decimal places.
    Rounding is NOT needed in my case as I am working with measurements in microns. Rounding to 1 micron makes very little difference, because the basic accuracy of the instrument is 3 microns of a mm.
    using the round() command also trims the string but does not add missing Zeros. However this can be used in my routine by un-remarking it.
    I have included some remarks for clarity

    Thanks to all for the input to make this work.
    Greetings from
    Friedrich, in Sunny South Africa
     

    Attached Files:

  10. Rioven

    Rioven Active Member Licensed User

    Hi Softselect,

    Nice string manipulations exercise!
    Can also use StrIndexOf(org,".",0) to return value of point location.;)

    Regards,
     
  11. Softselect

    Softselect Member Licensed User

    Hi Rioven,
    Thank for this infos
    I have updated my code with the use StrIndexOf(org,".",0) command
    and i also used it to seach for "." presence.
    Updated code attached

    Thank you
    Friedrich
     
  12. klaus

    klaus Expert Licensed User

    Hello Friedrich

    I agree with Rioven.

    You can also replace the line
    cut=StrInsert(cut,StrLength(cut),"0") 'add zeros
    by
    cut=cut&"0"

    because the zeros are at the end

    Klaus Switzerland
     
Loading...