dplsr
asked on
How do I stop a decimal from rounding off in asp.net?
I have a stored procedure returning a value of 34.7. when I display this in a label it is rounded off to 35.00. I tested the value in SQL Server Query Analyzer so I know I am stating off with 34.7. I assume I have a formating proplem.
here is my label code:
lblshipweight.Text = Format(myOrderDetails.Ship WeightTota l,"#00.00" )
here is the pertinent code from the stored procedure:
@shipweighttotal decimal(10,2) OUTPUT
SELECT
@shipweighttotal = Cast(SUM(CMRC_OrderDetails .Quantity * CMRC_OrderDetails.Weight) as decimal(10,2))
pertinent code from busines object (.vb file)
Dim myOrderDetails As New OrderDetails()
myOrderDetails.shipweightt otal = CDec(parametershipweightto tal.Value)
here is my label code:
lblshipweight.Text = Format(myOrderDetails.Ship
here is the pertinent code from the stored procedure:
@shipweighttotal decimal(10,2) OUTPUT
SELECT
@shipweighttotal = Cast(SUM(CMRC_OrderDetails
pertinent code from busines object (.vb file)
Dim myOrderDetails As New OrderDetails()
myOrderDetails.shipweightt
You could also use any of these:
lblshipweight.Text = myOrderDetails.ShipWeightT otal.ToStr ing("R")
lblshipweight.Text = myOrderDetails.ShipWeightT otal.ToStr ing("G2")
lblshipweight.Text = myOrderDetails.ShipWeightT otal.ToStr ing("0.##" )
etc.
See here for details:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconformattingoverview.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconnumericformatstrings.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconstandardnumericformatstrings.asp
lblshipweight.Text = myOrderDetails.ShipWeightT
lblshipweight.Text = myOrderDetails.ShipWeightT
lblshipweight.Text = myOrderDetails.ShipWeightT
etc.
See here for details:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconformattingoverview.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconnumericformatstrings.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconstandardnumericformatstrings.asp
ASKER
Sorry none of these work.
lblshipweight.Text = myOrderDetails.ShipWeightT otal.ToStr ing("R") format specifier was invalid
lblshipweight.Text = myOrderDetails.ShipWeightT otal.ToStr ing("G2")t returns 35
lblshipweight.Text = myOrderDetails.ShipWeightT otal.ToStr ing("0.##" ) returns 35
myOrderDetails.shipweightt otal = CType(parametershipweightt otal.Value ,Double)
lblshipweight.Text = myOrderDetails.ShipWeightT otal.ToStr ing("#,##0 .00;(#,##0 .00);") returns 35.00 should be 34.7
I tested with the Qery Analyzer again here are the results:
Output Parameter(s):
@shipweighttotal = 34.70
lblshipweight.Text = myOrderDetails.ShipWeightT
lblshipweight.Text = myOrderDetails.ShipWeightT
lblshipweight.Text = myOrderDetails.ShipWeightT
myOrderDetails.shipweightt
lblshipweight.Text = myOrderDetails.ShipWeightT
I tested with the Qery Analyzer again here are the results:
Output Parameter(s):
@shipweighttotal = 34.70
What type is the OrderDetails class? If it is a data table, check the type of the ShipWeightTotal column. Also try changing:
CDec(parametershipweightto tal.Value)
To:
CType(parametershipweightt otal.Value , Decimal)
I have no idea whether that CDec() vs CType() matters, but it's worth a try.
John
CDec(parametershipweightto
To:
CType(parametershipweightt
I have no idea whether that CDec() vs CType() matters, but it's worth a try.
John
ASKER
CType(parametershipweightt otal.Value , Decimal)
this also returns 35.00 instead of 34.7
ShipWeightTotal column is a decimal 9(18,2)
this also returns 35.00 instead of 34.7
ShipWeightTotal column is a decimal 9(18,2)
No, not the data type from the SQL Server table. Do you have a predefined dataset in your project that contains the OrderDetails data table? In your code you have:
Dim myOrderDetails As New OrderDetails()
What is the OrderDetails class? Can you view it in the object browser? I am thinking that it inherits from System.Data.DataTable. A DataTable has a Columns collection of DataColumn objects. It is the type of the DataColumn named ShipWeightTotal that I am asking about.
John
Dim myOrderDetails As New OrderDetails()
What is the OrderDetails class? Can you view it in the object browser? I am thinking that it inherits from System.Data.DataTable. A DataTable has a Columns collection of DataColumn objects. It is the type of the DataColumn named ShipWeightTotal that I am asking about.
John
How about converting into Double (as I showed) not Decimal ??
You need to leave it as a decimal, CDec it with the formatting then CStr it to your label
decShippingUSPS = CDec(decShippingUSPS.ToStr ing("$ ##.00"))
Me.lblShippingTotal.Text = CStr(decShippingUSPS)
Regards,
Aeros
decShippingUSPS = CDec(decShippingUSPS.ToStr
Me.lblShippingTotal.Text = CStr(decShippingUSPS)
Regards,
Aeros
Try to output the value directly to the Label without any formatting. Lets see what are you getting directly from ur class. I am guessing it is getting rounded there only.
-@Baan
-@Baan
ASKER
Goodmorning All!
I have tried the label with no formating. then I get 35.
In response to jnhorst, here is the complete function from my business object:
Public Function GetInvoiceDetails(orderID As Integer) As OrderDetails
' Create Instance of Connection and Command Object
Dim myConnection As New SqlConnection(Configuratio nSettings. AppSetting s("Connect ionString" ))
Dim myCommand As New SqlDataAdapter("CMRC_Invoi ceDetails" , myConnection)
' Mark the Command as a SPROC
myCommand.SelectCommand.Co mmandType = CommandType.StoredProcedur e
' Add Parameters to SPROC
Dim parameterOrderID As New SqlParameter("@OrderID", SqlDbType.Int, 4)
parameterOrderID.Value = orderID
myCommand.SelectCommand.Pa rameters.A dd(paramet erOrderID)
Dim parameterOrderDate As New SqlParameter("@OrderDate", SqlDbType.DateTime, 8)
parameterOrderDate.Directi on = ParameterDirection.Output
myCommand.SelectCommand.Pa rameters.A dd(paramet erOrderDat e)
Dim parameterShipDate As New SqlParameter("@ShipDate", SqlDbType.DateTime, 8)
parameterShipDate.Directio n = ParameterDirection.Output
myCommand.SelectCommand.Pa rameters.A dd(paramet erShipDate )
Dim parameterOrderTotal As New SqlParameter("@OrderTotal" , SqlDbType.Money, 8)
parameterOrderTotal.Direct ion = ParameterDirection.Output
myCommand.SelectCommand.Pa rameters.A dd(paramet erOrderTot al)
Dim parameterShipTotal As New SqlParameter("@ShipTotal", SqlDbType.Money, 8)
parameterShipTotal.Directi on = ParameterDirection.Output
myCommand.SelectCommand.Pa rameters.A dd(paramet erShipTota l)
Dim parametershipweighttotal As New SqlParameter("@shipweightt otal", SqlDbType.Decimal, 9,2)
parametershipweighttotal.D irection = ParameterDirection.Output
myCommand.SelectCommand.Pa rameters.A dd(paramet ershipweig httotal)
' Create and Fill the DataSet
Dim myDataSet As New DataSet()
myCommand.Fill(myDataSet, "OrderItems")
'ship date is null if order doesn't exist, or belongs to a different user
If Not parameterShipDate.Value Is DBNull.Value Then
' Create and Populate OrderDetails Struct using
'Output Params from the SPROC, as well as the
'populated dataset from the SqlDataAdapter
Dim myOrderDetails As New OrderDetails()
myOrderDetails.OrderDate = CType(parameterOrderDate.V alue, DateTime)
myOrderDetails.ShipDate = CType(parameterShipDate.Va lue, DateTime)
myOrderDetails.OrderTotal = CDec(parameterOrderTotal.V alue)
myOrderDetails.ShipTotal = CDec(parameterShipTotal.Va lue)
'myOrderDetails.shipweight total = CDec(parametershipweightto tal.Value)
myOrderDetails.shipweightt otal = CType(parametershipweightt otal.Value ,d
myOrderDetails.OrderItems = myDataSet
'Return the DataSet
Return myOrderDetails
Else
Return Nothing
End If
End Function
here is my stored procedure:
CREATE Procedure CMRC_InvoiceDetails
(
@OrderID int,
@OrderDate datetime OUTPUT,
@ShipDate datetime OUTPUT,
@OrderTotal money OUTPUT,
@ShipTotal money OUTPUT,
/*@shipweighttotal decimal(10,2) OUTPUT*/
@shipweighttotal money OUTPUT
)
AS
/* Return the order dates from the Orders
Also verifies the order exists for this customer. */
SELECT
@OrderDate = OrderDate,
@ShipDate = ShipDate,
@ShipTotal = shiptotal
FROM
CMRC_Orders
WHERE
OrderID = @OrderID
IF @@Rowcount = 1
BEGIN
/* First, return the OrderTotal out param */
SELECT
@OrderTotal = Cast(SUM(CMRC_OrderDetails .Quantity * CMRC_OrderDetails.UnitCost ) as money),
/* @shipweighttotal = Cast(SUM(CMRC_OrderDetails .Quantity * CMRC_OrderDetails.Weight) as decimal(10,2))*/
@shipweighttotal= sum(CMRC_OrderDetails.Quan tity * CMRC_OrderDetails.Weight)
FROM
CMRC_OrderDetails
WHERE
OrderID= @OrderID
/* Then, return the recordset of info */
SELECT
CMRC_Products.ProductID,
CMRC_Products.ModelName,
CMRC_Products.ModelNumber,
CMRC_OrderDetails.UnitCost ,
CMRC_OrderDetails.Quantity ,
CMRC_OrderDetails.Weight,
(CMRC_OrderDetails.Quantit y * CMRC_OrderDetails.UnitCost ) as ExtendedAmount
FROM
CMRC_OrderDetails
INNER JOIN CMRC_Products ON CMRC_OrderDetails.ProductI D = CMRC_Products.ProductID
WHERE
OrderID = @OrderID
select
customerID
FROM
CMRC_Orders
WHERE
OrderID = @OrderID
END
GO
and my label:
lblshipweight.Text = myOrderDetails.ShipWeightT otal.ToStr ing("#,##0 .00;(#,##0 .00);")
I have tried the label with no formating. then I get 35.
In response to jnhorst, here is the complete function from my business object:
Public Function GetInvoiceDetails(orderID As Integer) As OrderDetails
' Create Instance of Connection and Command Object
Dim myConnection As New SqlConnection(Configuratio
Dim myCommand As New SqlDataAdapter("CMRC_Invoi
' Mark the Command as a SPROC
myCommand.SelectCommand.Co
' Add Parameters to SPROC
Dim parameterOrderID As New SqlParameter("@OrderID", SqlDbType.Int, 4)
parameterOrderID.Value = orderID
myCommand.SelectCommand.Pa
Dim parameterOrderDate As New SqlParameter("@OrderDate",
parameterOrderDate.Directi
myCommand.SelectCommand.Pa
Dim parameterShipDate As New SqlParameter("@ShipDate", SqlDbType.DateTime, 8)
parameterShipDate.Directio
myCommand.SelectCommand.Pa
Dim parameterOrderTotal As New SqlParameter("@OrderTotal"
parameterOrderTotal.Direct
myCommand.SelectCommand.Pa
Dim parameterShipTotal As New SqlParameter("@ShipTotal",
parameterShipTotal.Directi
myCommand.SelectCommand.Pa
Dim parametershipweighttotal As New SqlParameter("@shipweightt
parametershipweighttotal.D
myCommand.SelectCommand.Pa
' Create and Fill the DataSet
Dim myDataSet As New DataSet()
myCommand.Fill(myDataSet, "OrderItems")
'ship date is null if order doesn't exist, or belongs to a different user
If Not parameterShipDate.Value Is DBNull.Value Then
' Create and Populate OrderDetails Struct using
'Output Params from the SPROC, as well as the
'populated dataset from the SqlDataAdapter
Dim myOrderDetails As New OrderDetails()
myOrderDetails.OrderDate = CType(parameterOrderDate.V
myOrderDetails.ShipDate = CType(parameterShipDate.Va
myOrderDetails.OrderTotal = CDec(parameterOrderTotal.V
myOrderDetails.ShipTotal = CDec(parameterShipTotal.Va
'myOrderDetails.shipweight
myOrderDetails.shipweightt
myOrderDetails.OrderItems = myDataSet
'Return the DataSet
Return myOrderDetails
Else
Return Nothing
End If
End Function
here is my stored procedure:
CREATE Procedure CMRC_InvoiceDetails
(
@OrderID int,
@OrderDate datetime OUTPUT,
@ShipDate datetime OUTPUT,
@OrderTotal money OUTPUT,
@ShipTotal money OUTPUT,
/*@shipweighttotal decimal(10,2) OUTPUT*/
@shipweighttotal money OUTPUT
)
AS
/* Return the order dates from the Orders
Also verifies the order exists for this customer. */
SELECT
@OrderDate = OrderDate,
@ShipDate = ShipDate,
@ShipTotal = shiptotal
FROM
CMRC_Orders
WHERE
OrderID = @OrderID
IF @@Rowcount = 1
BEGIN
/* First, return the OrderTotal out param */
SELECT
@OrderTotal = Cast(SUM(CMRC_OrderDetails
/* @shipweighttotal = Cast(SUM(CMRC_OrderDetails
@shipweighttotal= sum(CMRC_OrderDetails.Quan
FROM
CMRC_OrderDetails
WHERE
OrderID= @OrderID
/* Then, return the recordset of info */
SELECT
CMRC_Products.ProductID,
CMRC_Products.ModelName,
CMRC_Products.ModelNumber,
CMRC_OrderDetails.UnitCost
CMRC_OrderDetails.Quantity
CMRC_OrderDetails.Weight,
(CMRC_OrderDetails.Quantit
FROM
CMRC_OrderDetails
INNER JOIN CMRC_Products ON CMRC_OrderDetails.ProductI
WHERE
OrderID = @OrderID
select
customerID
FROM
CMRC_Orders
WHERE
OrderID = @OrderID
END
GO
and my label:
lblshipweight.Text = myOrderDetails.ShipWeightT
So OrderDetails is a struct with a ShipWeightTotal member. How is that member declared (what type)?
John
John
ASKER
Public Class OrderDetails
Public OrderDate As DateTime
Public ShipDate As DateTime
Public OrderTotal As Decimal
Public OrderItems As DataSet
Public CustomerID As Integer
Public intorderID As Integer
Public ShipTotal As Decimal
Public shipweighttotal As Decimal
End Class
Public OrderDate As DateTime
Public ShipDate As DateTime
Public OrderTotal As Decimal
Public OrderItems As DataSet
Public CustomerID As Integer
Public intorderID As Integer
Public ShipTotal As Decimal
Public shipweighttotal As Decimal
End Class
How about declaring them as double ?
-Baan
-Baan
ASKER
Goodmorning! All,
I finally fixed this problem. This may not be the proper way but, I changed the SQLDbType to Money,8 and did the same in the Stored Procedure. I am no getting the correct values.
I finally fixed this problem. This may not be the proper way but, I changed the SQLDbType to Money,8 and did the same in the Stored Procedure. I am no getting the correct values.
ASKER
Ok Thanks,
You may split the points between Experts, if that is done.
Thanks
You may split the points between Experts, if that is done.
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
use..
myOrderDetails.shipweightt
lblshipweight.Text = myOrderDetails.ShipWeightT
-@Baan