Solved

# How to round and truncate long decimal strings in vb.net

Posted on 2014-08-01
364 Views
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
0
Question by:sasllc
4 Comments

LVL 142

Assisted Solution

Please check double.parse and double.tryparse
0

LVL 21

Assisted Solution

``````Dim yv as double
yv=12.918171615141
yv=Math.Round(yv, 3, MidpointRounding.AwayFromZero)
``````
0

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
0

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()
else
yv = nothing
'string improperly formated, couldn't parse as a number.
end if

``````
0

## Join & Write a Comment Already a member? Login.

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.

#### Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!