Link to home
Start Free TrialLog in
Avatar of Larry Brister
Larry BristerFlag for United States of America

asked on

Truncate value in vb code .Net web page

I need to truncate a calculated decimal value in my vb code for my web page.

It needs to truncate after the second decimal point

so...
14.23956

would be set and shown as
14.23

And
2.369
would be shown as
2.36

This code only works if decimal value is > 10
rgsOPADtlPDRt.Text = Left(String.Format("{0:0.0000}", Double.Parse((rgsPaPDWgt * 1.0 / rgsPAPDPurchaseSum * 1.0)) * 100), 5)
Avatar of Mrunal
Mrunal
Flag of India image

try like this:

Dim test As Double = 2.3
Double.ToString("00.00", CultureInfo.InvariantCulture)

reference:

http://msdn.microsoft.com/en-us/library/0c899ak8.aspx#SpecifierPt
Try with FormatNumber(yourNumberVariable, 2)
Module Module1

    Sub Main()
        Dim x As Decimal
        x = 14.23956
        Dim x_string As String
        x_string = String.Format("{0:0.##}", x)
        Console.WriteLine(x_string)

        x = 2.369
        x_string = String.Format("{0:0.##}", x)
        Console.WriteLine(x_string)
    End Sub

End Module
Avatar of Larry Brister

ASKER

Hey guys
All of your examples rounded the number

using 2.369 in all suggestions returned 2.37

I need 2.36 returned.

I'm dealing with Amortization financial tables and they have been mandated to be TRUNCATED at the 4th decimal point

Therefore .02369 gets truncated to .0236
And displayed as 2.36
Another approach

double.parse(yournum.tostring.split(".")(0) & "." & yournum.tostring.padright("0", 2).substring(0, 2))
ASKER CERTIFIED SOLUTION
Avatar of Bardobrave
Bardobrave
Flag of Spain image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That did it!

Thanks