Larry Brister
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.0 000}", Double.Parse((rgsPaPDWgt * 1.0 / rgsPAPDPurchaseSum * 1.0)) * 100), 5)
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.0
Try with FormatNumber(yourNumberVar iable, 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
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
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
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.tostr ing.split( ".")(0) & "." & yournum.tostring.padright( "0", 2).substring(0, 2))
double.parse(yournum.tostr
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That did it!
Thanks
Thanks
Dim test As Double = 2.3
Double.ToString("00.00", CultureInfo.InvariantCultu
reference:
http://msdn.microsoft.com/en-us/library/0c899ak8.aspx#SpecifierPt