Solved

How do I stop a decimal from rounding off in asp.net?

Posted on 2004-09-10
18
713 Views
Last Modified: 2007-12-19
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.ShipWeightTotal,"#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.shipweighttotal = CDec(parametershipweighttotal.Value)
0
Comment
Question by:dplsr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 3
  • +3
18 Comments
 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 12031490
Hi there,
use..  
   myOrderDetails.shipweighttotal = CType(parametershipweighttotal.Value,Double)
   lblshipweight.Text = myOrderDetails.ShipWeightTotal.ToString("#,##0.00;(#,##0.00);")

-@Baan
0
 
LVL 24

Expert Comment

by:Justin_W
ID: 12031664
You could also use any of these:
   lblshipweight.Text = myOrderDetails.ShipWeightTotal.ToString("R")
   lblshipweight.Text = myOrderDetails.ShipWeightTotal.ToString("G2")
   lblshipweight.Text = myOrderDetails.ShipWeightTotal.ToString("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
0
 

Author Comment

by:dplsr
ID: 12032122
Sorry none of these work.

 lblshipweight.Text = myOrderDetails.ShipWeightTotal.ToString("R")  format specifier was invalid
   lblshipweight.Text = myOrderDetails.ShipWeightTotal.ToString("G2")treturns 35
   lblshipweight.Text = myOrderDetails.ShipWeightTotal.ToString("0.##")  returns 35

myOrderDetails.shipweighttotal = CType(parametershipweighttotal.Value,Double)
   lblshipweight.Text = myOrderDetails.ShipWeightTotal.ToString("#,##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
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 10

Expert Comment

by:jnhorst
ID: 12032242
What type is the OrderDetails class?  If it is a data table, check the type of the ShipWeightTotal column.  Also try changing:

CDec(parametershipweighttotal.Value)

To:

CType(parametershipweighttotal.Value, Decimal)

I have no idea whether that CDec() vs CType() matters, but it's worth a try.

John
0
 

Author Comment

by:dplsr
ID: 12032284
CType(parametershipweighttotal.Value, Decimal)

this also returns 35.00 instead of 34.7

ShipWeightTotal column is a decimal 9(18,2)
0
 
LVL 10

Expert Comment

by:jnhorst
ID: 12032317
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
0
 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 12032535
How about converting into Double (as I showed) not Decimal ??
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 12033072
You need to leave it as a decimal, CDec it with the formatting then CStr it to your label

decShippingUSPS = CDec(decShippingUSPS.ToString("$ ##.00"))
                Me.lblShippingTotal.Text = CStr(decShippingUSPS)

Regards,

Aeros
0
 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 12034351
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
0
 

Author Comment

by:dplsr
ID: 12034612
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(ConfigurationSettings.AppSettings("ConnectionString"))
            Dim myCommand As New SqlDataAdapter("CMRC_InvoiceDetails", myConnection)

            ' Mark the Command as a SPROC
            myCommand.SelectCommand.CommandType = CommandType.StoredProcedure

            ' Add Parameters to SPROC
            Dim parameterOrderID As New SqlParameter("@OrderID", SqlDbType.Int, 4)
            parameterOrderID.Value = orderID
            myCommand.SelectCommand.Parameters.Add(parameterOrderID)

            Dim parameterOrderDate As New SqlParameter("@OrderDate", SqlDbType.DateTime, 8)
            parameterOrderDate.Direction = ParameterDirection.Output
            myCommand.SelectCommand.Parameters.Add(parameterOrderDate)

            Dim parameterShipDate As New SqlParameter("@ShipDate", SqlDbType.DateTime, 8)
            parameterShipDate.Direction = ParameterDirection.Output
            myCommand.SelectCommand.Parameters.Add(parameterShipDate)

            Dim parameterOrderTotal As New SqlParameter("@OrderTotal", SqlDbType.Money, 8)
            parameterOrderTotal.Direction = ParameterDirection.Output
            myCommand.SelectCommand.Parameters.Add(parameterOrderTotal)
                
                  Dim parameterShipTotal As New SqlParameter("@ShipTotal", SqlDbType.Money, 8)
            parameterShipTotal.Direction = ParameterDirection.Output
            myCommand.SelectCommand.Parameters.Add(parameterShipTotal)
                  
                  Dim parametershipweighttotal As New SqlParameter("@shipweighttotal", SqlDbType.Decimal, 9,2)
            parametershipweighttotal.Direction = ParameterDirection.Output
            myCommand.SelectCommand.Parameters.Add(parametershipweighttotal)

            ' 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.Value, DateTime)
                myOrderDetails.ShipDate = CType(parameterShipDate.Value, DateTime)
                myOrderDetails.OrderTotal = CDec(parameterOrderTotal.Value)
       myOrderDetails.ShipTotal = CDec(parameterShipTotal.Value)
        'myOrderDetails.shipweighttotal = CDec(parametershipweighttotal.Value)
       myOrderDetails.shipweighttotal = CType(parametershipweighttotal.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.Quantity * 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.Quantity * CMRC_OrderDetails.UnitCost) as ExtendedAmount

FROM
    CMRC_OrderDetails
  INNER JOIN CMRC_Products ON CMRC_OrderDetails.ProductID = CMRC_Products.ProductID
 
WHERE  
    OrderID = @OrderID

select
 customerID
FROM    
    CMRC_Orders
   
WHERE  
    OrderID = @OrderID

END
GO


and my label:
lblshipweight.Text = myOrderDetails.ShipWeightTotal.ToString("#,##0.00;(#,##0.00);")
0
 
LVL 10

Expert Comment

by:jnhorst
ID: 12035012
So OrderDetails is a struct with a ShipWeightTotal member.  How is that member declared (what type)?

John
0
 

Author Comment

by:dplsr
ID: 12038400
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
0
 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 12038750
How about declaring them as double ?
-Baan
0
 

Author Comment

by:dplsr
ID: 12043423
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.
0
 

Author Comment

by:dplsr
ID: 12048697
Ok Thanks,
You may split the points between Experts, if that is done.
Thanks
0
 

Accepted Solution

by:
ee_ai_construct earned 0 total points
ID: 12083850
Please read entire thread.  Asker solved or content important.
Closed, 500 points refunded.
ee_ai_construct (replacement part #xm34)
Community Support Admin
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

636 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