How to round and truncate long decimal strings in for compact framework

Posted on 2014-08-01
Last Modified: 2014-08-11
Earlier today I asked this question:

I will have string data coming in, with 1, 2, or 3 digits preceding the decimal point, and 12 digits after the decimal point.  An example would be 12.918171615141.  I need to truncate everything past the 4th decimal digit, so that my example string would look like 12.9181.

Better yet would be if I could ROUND it properly, so that in this example I would end up with 12.9182.  But to do so, I assume I would have to move the string to a numeric variable and use a math function.  IF this is feasible, I need some sample code to handle this as well, i.e. how to properly get it into a numeric variable, then do the rounding, and get it back in to a string.  TIA

And the most complete answer I got was this:

Dim yv as Double

if Double.TryParse(strABC, out yv) then
     'strABC a valid decimal.
     yv = Math.Round(yv, 3, MidpointRounding.AwayFromZero)
     strABC = yv.ToString()
    yv = nothing
end if

I closed out the question too soon, because now that I'm able to try this code, I am getting syntax errors.  I suspect that some of this may be because I'm working in the compact framework environment with its limitations.  Unfortunately I failed to mention this in my earlier question.

Specifically, here are the errors I am getting:

1) In the line that says "if Double.TryParse(Me.txtLocTot.Text, out yv) then", it is saying that "TryParse is not a member of Double", and "name out is not declared", and "comma or valid expression expected" at the end of that line.

2) In the line that says "yv = Math.Round(yv, 3, MidpointRounding.AwayFromZero)", it says "MidpointRounding is not declared".

I have messed with it for quite a while, but getting nowhere.  So, any suggestions to will be appreciated.  TIA
Question by:sasllc
    LVL 62

    Accepted Solution

    Hi sasllc;

    The following code is working on my box but could not Test on Compact framework but should work on it as well.

    Dim yv As Double 
    if Double.TryParse(strABC, yv) then
         yv = Math.Round(yv, 4, MidpointRounding.AwayFromZero)
         strABC = yv.ToString()
        '' If the string could not be converted to a double it returns a zero in yv but it that is not expectable 
        '' make it some negative value.
        yv = -1
    end if

    Open in new window

    LVL 3

    Author Comment

    The syntax errors I get are these:

    'TryParse' is not a member of 'Double'.

    Name 'MidpointRounding' is not declared.

    Any idea how to get around these two problems?  I am using Visual Studio 2008, developing for a mobile device.
    LVL 62

    Expert Comment

    by:Fernando Soto
    I have VS 2013 and tried it on a project using mobile device template and it does compile. I tried looking for documentation but could not find anything specific to mobile api support. I do not know if I will be of any help with this issue.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
    Article by: Kraeven
    Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
    This video discusses moving either the default database or any database to a new volume.
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    794 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now