Sunday, February 19, 2012

How to define field attribute for a numeric field in SQL table?

I need create a field to store tax rate. I need only 2 decimal points. I defined the field as decimal, precision=5 and scale=2. Does it mean that it can hold value from 0.00 to 999.99?Actually, it should be able to hold from -999.99 through 999.99.

Terri|||True... here comes my really problem. My aspx form always does rounding for me while I update the table. If I enter 0.7 it will become 0 in the table. If I enter 2.7 it will become 3 in the table. What is the trick?|||You'll have to show us your code surrounding the SQL command. It's probably in how your data type is set up in your SQLParameter or something along those lines. There's no trick from a SQL perspective -- a command from Query Analyzer would simply look like this:


UPDATE test SET taxRate = '.7' WHERE ID = 10

Terri|||Try Numeric it will make the rounding problem go away in SQL Server, in .NET search MSDN for Strings and Formating in your language. Numeric is bigger than Decimal. Hope this helps.

Kind regards,
Gift Peddie|||This is how I coded...

First is the class


Namespace BusinessLogicLayer
Public Class Itm
...
Private _PST As Decimal
...
Public Property PST() As Decimal
Get
Return _PST
End Get
Set(ByVal Value As Decimal)
_PST = Value
End Set
End Property
...
...
Public Function Insert() As Boolean
ItmID = Convert.ToInt32(SqlHelper.ExecuteScalar(ConfigurationSettings.AppSettings(Web.Global.CfgKeyConnString), "AddItm", CatID, ItmD1, ItmD2, UOM, UPrice, PSTPcnt, GSTPcnt, Note, MasterItem))
Return ItmID > 0
End Function 'Insert
...
End Class
End Namespace

then page load sub...

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
...
If txtPSTPcnt.Text = "" Then
txtPSTPcnt.Text = "0.07"
End If
...
End Sub

then when the Save button is clicked


Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
...
Itm.PST = CDec(txtPST.Text)
Itm.Insert()
End Sub

Hopefully you have some clue, thx.|||I changed it to numeric and still have the same result.|||Sorry I did not get back to you yesterday, you need code in Custom Numeric Format Strings with using statement from System.globalization. I found some code using the IFormatProvider and ICustomFormatter interfaces on codeproject site. The second link is on MSDN Custom Numeric Format Strings Output Examples table you may have to use DOUBLE datatype in .NET. Hope this helps.

http://www.codeproject.com
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconcustomnumericformatstrings.asp

Kind regards,
Gift Peddie|||Sorry this is the table that show the DOUBLE datatypes human error lol. Hope this helps

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconcustomnumericformatstringsoutputexample.asp

Kind regards,
Gift Peddie|||Thanks a lot. you're very helpful. Since this is my first project on ASP.net and SQL, I can't grap the idea of using the table you refer to. First of all, where do I define "double" and do I put those code in the ASPX.VB? Appreciated.|||The code below should take you closer to what you want, these are part of the .NET Framework Class Library that perform special tasks for you. I don't know books that covers it in VB.NET but in C# the Complete Reference by Herbert Schildt covered it in details in C# not Asp.net. Hope this helps.

NumberFormatInfo.PercentDecimalSeparator Property
[Visual Basic]the effect of changing the PercentDecimalSeparator property.

[Visual Basic]
Imports System
Imports System.Globalization

Class NumberFormatInfoSample

Public Shared Sub Main()

' Gets a NumberFormatInfo associated with the en-US culture.
Dim nfi As NumberFormatInfo = New CultureInfo("en-US", False).NumberFormat

' Displays a value with the default separator (".").
Dim myInt As [Double] = 0.1234
Console.WriteLine(myInt.ToString("P", nfi))

' Displays the same value with a blank as the separator.
nfi.PercentDecimalSeparator = " "
Console.WriteLine(myInt.ToString("P", nfi))

End Sub 'Main

End Class 'NumberFormatInfoSample

'This code produces the following output.

'

'12.34 %

'12 34 %

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemglobalizationnumberformatinfoclasspercentdecimalseparatortopic.asp

Kind regards,
Gift Peddie|||Again thanks for your advice, but I can't find data type Double in SQL2000. Did I miss something?|||Gentlmen, I found where the problem occured. It's because the SQL parameter wasn't defined properly. Instead of saying


(
@.PST decimal(5,2)
)

I only typed


(
@.PST decimal
)

Anyway I thank anyone participated in the threat.

No comments:

Post a Comment