How to round and truncate long decimal strings in

Posted on 2014-08-01
Last Modified: 2014-08-01
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
Question by:sasllc
    LVL 142

    Assisted Solution

    by:Guy Hengel [angelIII / a3]
    Please check double.parse and double.tryparse
    LVL 21

    Assisted Solution

    by:Randy Poole
    Dim yv as double
    yv=Math.Round(yv, 3, MidpointRounding.AwayFromZero)

    Open in new window

    LVL 3

    Author Comment

    In this example from Randy Poole, what is the right way to get my string into the numeric variable?  If my string is named strABC, do I simply say "yv=strABC"...or what is best?  

    I really do need some sample showing me how to properly get the string into a numeric variable, then do the rounding, and get it back in to a string
    LVL 39

    Accepted Solution

    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
       'string improperly formated, couldn't parse as a number.
    end if

    Open in new window


    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    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 …
    I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    754 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

    18 Experts available now in Live!

    Get 1:1 Help Now