ปัญหาของผมคือ ผมจะทำยังไงให้เลขที่ได้มาแบบต่างๆ เข้า format เพื่อ บวก ลบ ได้ตรง ครับ format มีอยู่ประมาณ 5แบบครับ
ตอนนี้ผมทำแต่แค่ format ที่เลขมาได้แบบนี้อย่างเดียวครับ แบบอื่นยังทำไม่ได้จริงๆครับ
Code (VB.NET)
For i As Integer = 0 To dtformula.Rows.Count - 1
TSM_FORMULA_OLD = dtformula.Rows(i).Item("TSM_FORMULA") 'TSM_FORMULA_OLD = 0.20 + 0.15
If TSM_FORMULA_OLD <> "" Then
Dim Checknum As String = TSM_FORMULA_OLD
'Dim regex As String = System.Text.RegularExpressions.Regex.Replace(TSM_FORMULA_OLD, "^\d+[.]\d{2}$", " ")
If Checknum.ToString.Contains("0.") And Checknum.ToString.Contains("+0.") Or Checknum.ToString.Contains("-0.") = True Then
Dim strnum1 As Integer = TSM_FORMULA_OLD.Substring(2, 2)
Dim strnum2 As Integer = TSM_FORMULA_OLD.Substring(7, 2)
Dim strsign1 As String = TSM_FORMULA_OLD.Substring(4, 1)
Dim Sumformula As String
Sumformula = "0." & Convert.ToInt32(strnum1) - 1 & strsign1 & "0." & Convert.ToInt32(strnum2) - 1
TSM_FORMULA_NEW = Sumformula
End If
End If
Next
If dtformula.Rows.Count > 0 Then
For i As Integer = 0 To dtformula.Rows.Count - 1
TSM_FORMULA_OLD = dtformula.Rows(i).Item("TSM_FORMULA")
If TSM_FORMULA_OLD <> "" Then
Dim Getnum As String = System.Text.RegularExpressions.Regex.Replace(TSM_FORMULA_OLD, "[+,-]", " ") '"\d+[+,-]
Dim Getsign As String = System.Text.RegularExpressions.Regex.Replace(TSM_FORMULA_OLD, "\d+", " ")
'If IsNumeric(Getnum) And Getsign.Contains("+") Or Getsign.Contains("-") = True Then
' Dim Getsearchstr1 As String = System.Text.RegularExpressions.Regex.Replace(TSM_FORMULA_OLD, "0.", "0.")
' If Getsearchstr1.Contains(Getsearchstr1) > 0 Then
' Dim pattern As String = "[+,-]"
' Dim input As String = TSM_FORMULA_OLD
' Dim result() As String = Regex.Split(input, pattern)
' For ctr As Integer = 0 To result.Length - 1
' Console.Write("'{0}'", result(ctr))
' If ctr < result.Length - 1 Then Console.Write("")
' Dim GetResultnumber1 As String
' Dim GetResultnumber2 As String
' Dim GetResultnumber3 As String
' Dim GetResultnumber4 As String
' Dim GetResultnumber5 As String
' Dim GetResultnumber6 As String
' If result.Length = 1 Then
' GetResultnumber1 = result(0)
' End If
' If result.Length = 2 Then
' GetResultnumber1 = result(0)
' GetResultnumber2 = result(1)
' End If
' If result.Length = 3 Then
' GetResultnumber1 = result(0)
' GetResultnumber2 = result(1)
' GetResultnumber3 = result(2)
' End If
' If result.Length = 4 Then
' GetResultnumber1 = result(0)
' GetResultnumber2 = result(1)
' GetResultnumber3 = result(2)
' GetResultnumber4 = result(3)
' End If
' If result.Length = 5 Then
' GetResultnumber1 = result(0)
' GetResultnumber2 = result(1)
' GetResultnumber3 = result(2)
' GetResultnumber4 = result(3)
' GetResultnumber5 = result(4)
' End If
' If result.Length = 6 Then
' GetResultnumber1 = result(0)
' GetResultnumber2 = result(1)
' GetResultnumber3 = result(2)
' GetResultnumber4 = result(3)
' GetResultnumber5 = result(4)
' GetResultnumber6 = result(5)
' End If
' If GetResultnumber1 <> "" Then
' GetResultnumber1 = Convert.ToInt32(GetResultnumber1) - 1
' End If
' If GetResultnumber2 <> "" Then
' GetResultnumber2 = Convert.ToInt32(GetResultnumber2) - 1
' End If
' If GetResultnumber3 <> "" Then
' GetResultnumber3 = Convert.ToInt32(GetResultnumber3) - 1
' End If
' If GetResultnumber4 <> "" Then
' GetResultnumber4 = Convert.ToInt32(GetResultnumber4) - 1
' End If
' If GetResultnumber5 <> "" Then
' GetResultnumber5 = Convert.ToInt32(GetResultnumber5) - 1
' End If
' If GetResultnumber6 <> "" Then
' GetResultnumber6 = Convert.ToInt32(GetResultnumber6) - 1
' End If
' Console.WriteLine()
' Dim patternsign As String = "\d+"
' Dim inputsign As String = TSM_FORMULA_OLD
' Dim resultsign() As String = Regex.Split(inputsign, patternsign)
' For ctrsign As Integer = 0 To resultsign.Length - 1
' Console.Write("'{0}'", resultsign(ctrsign))
' If ctrsign < resultsign.Length - 1 Then Console.Write("")
' Dim GetResultsign1 As String = ""
' Dim GetResultsign2 As String = ""
' Dim GetResultsign3 As String = ""
' Dim GetResultsign4 As String = ""
' Dim GetResultsign5 As String = ""
' Dim GetResultsign6 As String = ""
' If resultsign.Length = 2 Then
' GetResultsign1 = resultsign(1)
' End If
' If resultsign.Length = 3 Then
' GetResultsign1 = resultsign(1)
' GetResultsign2 = resultsign(2)
' End If
' If resultsign.Length = 4 Then
' GetResultsign1 = resultsign(1)
' GetResultsign2 = resultsign(2)
' GetResultsign3 = resultsign(3)
' End If
' If resultsign.Length = 5 Then
' GetResultsign5 = resultsign(0)
' GetResultsign1 = resultsign(1)
' GetResultsign2 = resultsign(2)
' GetResultsign3 = resultsign(3)
' End If
' If resultsign.Length = 6 Then
' GetResultsign5 = resultsign(0)
' GetResultsign1 = resultsign(1)
' GetResultsign2 = resultsign(2)
' GetResultsign3 = resultsign(3)
' GetResultsign4 = resultsign(4)
' End If
' Console.WriteLine()
' Dim Sumformula As String
' Sumformula = GetResultnumber1 & GetResultsign1 & GetResultnumber2 & GetResultsign2 & GetResultnumber3 & GetResultsign3 & GetResultnumber4 & GetResultsign4 & GetResultnumber5 & GetResultsign5 & GetResultnumber6
' TSM_FORMULA_NEW = Sumformula
' Next
' Next
' End If
'End If
If IsNumeric(Getnum) And Getsign.Contains("+") Or Getsign.Contains("-") = True Then
Dim pattern As String = "[+-]"
Dim input As String = TSM_FORMULA_OLD
Dim result() As String = Regex.Split(input, pattern)
For ctr As Integer = 0 To result.Length - 1
Console.Write("'{0}'", result(ctr))
If ctr < result.Length - 1 Then Console.Write("")
Dim GetResultnumber1 As String
Dim GetResultnumber2 As String
Dim GetResultnumber3 As String
Dim GetResultnumber4 As String
Dim GetResultnumber5 As String
Dim GetResultnumber6 As String
If result.Length = 1 Then
GetResultnumber1 = result(0)
End If
If result.Length = 2 Then
GetResultnumber1 = result(0)
GetResultnumber2 = result(1)
End If
If result.Length = 3 Then
GetResultnumber1 = result(0)
GetResultnumber2 = result(1)
GetResultnumber3 = result(2)
End If
If result.Length = 4 Then
GetResultnumber1 = result(0)
GetResultnumber2 = result(1)
GetResultnumber3 = result(2)
GetResultnumber4 = result(3)
End If
If result.Length = 5 Then
GetResultnumber1 = result(0)
GetResultnumber2 = result(1)
GetResultnumber3 = result(2)
GetResultnumber4 = result(3)
GetResultnumber5 = result(4)
End If
If result.Length = 6 Then
GetResultnumber1 = result(0)
GetResultnumber2 = result(1)
GetResultnumber3 = result(2)
GetResultnumber4 = result(3)
GetResultnumber5 = result(4)
GetResultnumber6 = result(5)
End If
If GetResultnumber1 <> "" Then
GetResultnumber1 = Convert.ToDouble(GetResultnumber1)
If GetResultnumber1.Length = "4" And GetResultnumber1.Contains("1") Then
If GetResultnumber1.Contains("0.1") = False Then
GetResultnumber1 = GetResultnumber1 - 0.01 & "0"
Else : GetResultnumber1 = GetResultnumber1 - 0.01
End If
Else : GetResultnumber1 = GetResultnumber1 - 0.01
End If
End If
If GetResultnumber2 <> "" Then
GetResultnumber2 = Convert.ToDouble(GetResultnumber2)
If GetResultnumber2.Length = "4" And GetResultnumber2.Contains("1") Then
If GetResultnumber2.Contains("0.1") = False Then
GetResultnumber2 = GetResultnumber2 - 0.01 & "0"
Else : GetResultnumber2 = GetResultnumber2 - 0.01
End If
Else : GetResultnumber2 = GetResultnumber2 - 0.01
End If
End If
If GetResultnumber3 <> "" Then
GetResultnumber3 = Convert.ToDouble(GetResultnumber3)
If GetResultnumber3.Length = "4" And GetResultnumber3.Contains("1") Then
If GetResultnumber3.Contains("0.1") = False Then
GetResultnumber3 = GetResultnumber3 - 0.01 & "0"
Else : GetResultnumber3 = GetResultnumber3 - 0.01
End If
Else : GetResultnumber3 = GetResultnumber3 - 0.01
End If
End If
If GetResultnumber4 <> "" Then
GetResultnumber4 = Convert.ToDouble(GetResultnumber4)
If GetResultnumber4.Length = "4" And GetResultnumber4.Contains("1") Then
If GetResultnumber4.Contains("0.1") = False Then
GetResultnumber4 = GetResultnumber4 - 0.01 & "0"
Else : GetResultnumber4 = GetResultnumber4 - 0.01
End If
Else : GetResultnumber4 = GetResultnumber4 - 0.01
End If
End If
If GetResultnumber5 <> "" Then
GetResultnumber5 = Convert.ToDouble(GetResultnumber5)
If GetResultnumber5.Length = "4" And GetResultnumber5.Contains("1") Then
If GetResultnumber5.Contains("0.1") = False Then
GetResultnumber5 = GetResultnumber5 - 0.01 & "0"
Else : GetResultnumber5 = GetResultnumber5 - 0.01
End If
Else : GetResultnumber5 = GetResultnumber5 - 0.01
End If
End If
If GetResultnumber6 <> "" Then
GetResultnumber6 = Convert.ToDouble(GetResultnumber6) - 0.01
If GetResultnumber6.Length = "4" And GetResultnumber6.Contains("1") Then
If GetResultnumber6.Contains("0.1") = False Then
GetResultnumber6 = GetResultnumber6 - 0.01 & "0"
Else : GetResultnumber6 = GetResultnumber6 - 0.01
End If
Else : GetResultnumber6 = GetResultnumber6 - 0.01
End If
End If
Console.WriteLine()
Dim patternsign As String = "\d+.\d+"
Dim inputsign As String = TSM_FORMULA_OLD
Dim resultsign() As String = Regex.Split(inputsign, patternsign)
For ctrsign As Integer = 0 To resultsign.Length - 1
Console.Write("'{0}'", resultsign(ctrsign))
If ctrsign < resultsign.Length - 1 Then Console.Write("")
Dim GetResultsign1 As String = ""
Dim GetResultsign2 As String = ""
Dim GetResultsign3 As String = ""
Dim GetResultsign4 As String = ""
Dim GetResultsign5 As String = ""
Dim GetResultsign6 As String = ""
If resultsign.Length = 2 Then
GetResultsign1 = resultsign(1)
End If
If resultsign.Length = 3 Then
GetResultsign1 = resultsign(1)
GetResultsign2 = resultsign(2)
End If
If resultsign.Length = 4 Then
GetResultsign1 = resultsign(1)
GetResultsign2 = resultsign(2)
GetResultsign3 = resultsign(3)
End If
If resultsign.Length = 5 Then
GetResultsign1 = resultsign(1)
GetResultsign2 = resultsign(2)
GetResultsign3 = resultsign(3)
GetResultsign4 = resultsign(4)
End If
If resultsign.Length = 6 Then
GetResultsign1 = resultsign(1)
GetResultsign2 = resultsign(2)
GetResultsign3 = resultsign(3)
GetResultsign4 = resultsign(4)
GetResultsign5 = resultsign(5)
End If
If resultsign.Length = 7 Then
GetResultsign1 = resultsign(1)
GetResultsign2 = resultsign(2)
GetResultsign3 = resultsign(3)
GetResultsign4 = resultsign(4)
GetResultsign5 = resultsign(5)
GetResultsign6 = resultsign(6)
End If
Console.WriteLine()
Dim Sumformula As String
Sumformula = GetResultnumber1 & GetResultsign1 & GetResultnumber2 & GetResultsign2 & GetResultnumber3 & GetResultsign3 & GetResultnumber4 & GetResultsign4 & GetResultnumber5 & GetResultsign5 & GetResultnumber6
TSM_FORMULA_NEW = Sumformula
Next
Next
End If
End If
Next
End If
ย้า id ไม่ใช่ No
no สามารถ เปลี่ยนแปลง ได้ตามการคำนวณ กรณ๊อยาก repair ให้เลขมันเรียงลำดับกัน
แต่ id จะไม่เปลี่ยนแปลงตามการ คำนวณ เป็น auto increment เพื่อที่การคำนวณจะได้ถูกต้อง
การแสดงผล no คุณต้องคิวรี่ formula จาก id แล้ว เอา no มาแสดง
ผมว่าประเด็นน่าจะอยู่ที่ ต้องทำการแก้ไข field formula ขนานไปกับการแก้ field no
เช่นถ้าแก้ no 2 เป็น 1 ก็ต้องค้นหาอัพเดท formula ที่อ้างอิง no2 ให้แก้เป็น 1 ไปพร้อมๆ กัน
update p, c
set
c.formular = replace( c.formula, concat('0.',p.no), concat('0.', p.no-1 )
, p.no=p.no-1
from tablename as p
inner join tablename as c
on c.formula like concat('%0.',p.no,'%')
where p.no>?
ใช่ครับ แต่ผมอยากรู้ว่าจะทำยังไงให้ -1 ให้มันตรง format มันได้อย่างไร ผมติดตรงที่ format มันนี่ละครับ ไม่รู้จะแยกยังไง และเมื่อมันมารวมๆกัน ก็ไม่รู้จะแยก format - ยังไงเหมือนกัน
Dim sentence As String = TSM_FORMULA_OLD 'สมมุติเลข 16.4.1+16+17
Dim digits() As String = Regex.Split(sentence, "\d+") 'แยก arrayได้ array(0) = "" , array(1) = "." , array(2) = "." , array(3) = "+" , array(4) = "+" , array(5) = ""
Dim GetResultSign1 As String
Dim GetResultSign2 As String
Dim GetResultSign3 As String
Dim GetResultSign4 As String
Dim GetResultSign5 As String
Dim Getcharacter As String
Dim Getcha() As String
Dim Getsumsign() As String
For GetChardigits As Integer = 0 To digits.Length - 1
If digits.Contains("+") Or digits.Contains("-") Then
Getcharacter = digits(GetChardigits)
If Getcharacter = "+" Or Getcharacter = "-" Then
Getcha(Getcharacter) = Getsumsign(Getcharacter)
End If
End If
Next
Dim strRegex as String = "([+-]?[\d\.]{1,})"
Dim myRegex As New Regex(strRegex, RegexOptions.None)
Dim strTargetString As String = "8.2.1-8.2.2+8.2.3+8.2.4"
For Each myMatch As Match In myRegex.Matches(strTargetString)
If myMatch.Success Then
' Add your code here
End If
Next
Dim strRegex as String = "([\+-\.])"
Dim myRegex As New Regex(strRegex, RegexOptions.None)
Dim strTargetString As String = "8.2.1-8.2.2+8.2.3+8.2.4"
For Each myMatch As Match In myRegex.Matches(strTargetString)
If myMatch.Success Then
' Add your code here
End If
Next
Dim strRegex as String = "([\+-\.])"
Dim myRegex As New Regex(strRegex, RegexOptions.None)
Dim strTargetString As String = "8.2.1-8.2.2+8.2.3+8.2.4"
For Each myMatch As Match In myRegex.Matches(strTargetString)
If myMatch.Success Then
' Add your code here
End If
Next