RSS

Monthly Archives: March 2012

民國年與西元年之間的轉換與顯示

很幸運我最近剛開始寫到日期字串處理的時候就碰到閏年的問題,因為今年正好是閏年。

當程式裡需要將民國年的字串轉換成DateTime格式的時候,我使用了土法煉鋼的方式,Date.Parse().AddYear(1911),先轉換再加1911年。還好沒幾天馬上碰到101/2/29!當Date.Parse(String)的時候,其實是將字串轉換為西元年,西元101年當然沒有2/29號…因此會出現”字串未被辨認為有效的 DateTime”Exception。而且這麼做在民國99年(含)以前出來的日期也全部錯誤。先將字串的年份加上1911也許是可行的方法,但是會有多餘的型態轉換,而且不夠彈性。

.NET本身就有對應世界各國日曆格式的方式,可以使用Date.Parse(String, IFormatProvider)Date.ToString(IFormatProvider),IFormatProvider參數可以是CultureInfoDateTimeFormatInfo,在其中指定DateTimeFormatInfo.Calendar屬性為TaiwanCalendar,就可以正確的轉換與顯示民國年。

另外需要注意的是,DateTime.Year屬性所傳回的是西曆年份,如果要套用不同的日曆格式,要使用所指定曆法的GetYear方法。在DateTime.Year裡有MSDN的範例說明。

以下是一個簡單的範例,可以比較錯誤與正確的轉換方式和結果:

Read the rest of this entry »

 
Leave a comment

Posted by on March 23, 2012 in .NET, VB

 

Tags:

VB Left() and Right() equivalent in VB.NET

When trying to rewrite VB string manipulation codes into .NET framework version, old VB Left() and Right() methods could be seen in the lines very often. At first I rewrote them as :

'Declaration
'Public Function Substring ( _
'    startIndex As Integer, _
'    length As Integer _
') As String

' str = Left(str, length) equivalent
str = str.Substring(0, length)
' str = Right(str, length) equivalent
str = str.Substring(str.Length - length)

Generally this works fine but could potentially cause startIndex and length out of range exception in conditions like:

Dim str As String = "01234"
str = Left(str, 6)           'str = "01234"
str = str.Substring(0, 6)    'Exception

There’s a much more feasible solution on stackoverflow by Noldorin which uses Math.Min() for Left() to make sure the out of range exception won’t happen. Here are the refined equivalents according to Noldorin’s solution:

' str = Left(str, length) equivalent
str = str.Substring(0, Math.Min(length, str.Length))
' str = Right(str, length) equivalent
str = str.Substring(Math.Max(str.Length, length) - length)
 
Leave a comment

Posted by on March 20, 2012 in .NET, VB

 

Tags:

Convert number to words in English

The methods below are rewritten from How to convert a numeric value into English words in Excel to VB.NET equivalents. It takes string type arguments of numbers like “12,345.6789”, rounds it down to the second decimal place which becomes “12,345.67” and spells it as “Twelve Thousand Three Hundred Forty Five Dollars and Sixty Seven Cents”.

Read the rest of this entry »

 
Leave a comment

Posted by on March 20, 2012 in .NET, VB

 

Tags:

Why is DataGridView slow

I have an about 100 rows DataGridView which drops performance significantly since I wrote some code modifying its cells value programmatically. After looking up the MSDN post, the property AutoSizeColumnsMode is the reason why my DataGridView being slow. The property was set to AllCells, turns out it’ll auto resize cells when the value changes and that takes a lot of resource.

By 100 rows my code will run for about 1500 milliseconds. Changing the property to DisplayedCells can reduce it to 30 milliseconds and changing to ColumnHeader can reduce it to less than 8 milliseconds. It’ll cost no time if I commented the value changing code.

I guess every single cell value change will always auto resize according to the value of the AutoSize property, even when the property value is ColumnHeader.

 
Leave a comment

Posted by on March 13, 2012 in .NET, Winform

 

Tags: , ,

How to get the integer part of a given number

In Visual Basic we could use Fix() and Int() to get the integer part of a given numeric value. But they handle negative values differently, I’ll simply show some examples as follows:

number Fix(number) Int(number)
12.2 12 12
11.8 11 11
-9.3 -9 -10
-9.8 -9 -10
-10.2 -10 -11

Here is the code to compare both function:

Private Sub TestGetIntegerPart()
    Dim numbers() As Double = {12.2, 11.8, -9.3, -9.8, -10.2}

    Dim dt As New DataTable
    dt.Columns.Add("number")
    dt.Columns.Add("Fix")
    dt.Columns.Add("Int")
    Dim row As DataRow

    For Each num As Double In numbers
        row = dt.NewRow()
        row("number") = num
        row("Fix") = Fix(num)
        row("Int") = Int(num)

        dt.Rows.Add(row)
    Next

    Me.DataGridView1.DataSource = dt
End Sub
 
Leave a comment

Posted by on March 8, 2012 in .NET, VB

 

Tags:

將阿拉伯數字金額轉換成國字金額

處理銀行支票的時候,需要將阿拉伯數字轉換為國字,以下這個function可以將一般的金額轉換為國字金額,數值可以包含”,”和”.”,但小數點下必須是零:

Read the rest of this entry »

 
Leave a comment

Posted by on March 8, 2012 in .NET, VB

 

Tags:

Prompt a MessageBox for confirmation

I need a MessageBox with OK and Cancel buttons for users to confirm. Here is an easy sample:

Dim result As DialogResult
result = MessageBox.Show("Are you sure this is OK?", "MyTitle", MessageBoxButtons.OKCancel, Nothing, MessageBoxDefaultButton.Button1)

If result = DialogResult.OK Then
    ' User presses OK button.
    ' Do something.
End If
 
Leave a comment

Posted by on March 6, 2012 in .NET, VB

 

Tags: