fork(4) download
  1. Imports System
  2.  
  3. Public Class Test
  4. Public Shared Sub Main()
  5. Dim fd As New FactorDecimal(2)
  6. Dim Input As String = "415276"
  7.  
  8. Console.WriteLine("Input: " & Input)
  9.  
  10. 'Adding characters.'
  11. For Each c As Char In Input
  12. fd.AppendNumber(c)
  13. Next
  14.  
  15. Console.WriteLine("Result: " & fd.ToString())
  16. Console.WriteLine()
  17.  
  18. 'Removing characters "152" from "415276".'
  19. fd.RemoveRange(1, 3)
  20.  
  21. Console.WriteLine("RemoveRange(1, 3): " & fd.ToString())
  22. End Sub
  23. End Class
  24.  
  25. Public Class FactorDecimal
  26. Private _value As String = "0"
  27.  
  28. Public DecimalPlaces As Integer
  29.  
  30. Public Sub AppendNumber(ByVal Character As Char)
  31. If Char.IsNumber(Character) = False Then Throw New ArgumentException("Input must be a valid numerical character!", "Character")
  32. _value = (_value & Character).TrimStart("0"c)
  33. End Sub
  34.  
  35. Public Sub RemoveRange(ByVal Index As Integer, ByVal Length As Integer)
  36. If _value.Length >= Me.DecimalPlaces + 1 AndAlso _
  37. Index + Length > _value.Length - Me.DecimalPlaces Then Length -= 1 'Exclude decimal point.'
  38.  
  39. If Index + Length >= _value.Length Then Length = _value.Length - Index 'Out of range checking.'
  40.  
  41. _value = _value.Remove(Index, Length)
  42. If _value.Length = 0 Then _value = "0"
  43. End Sub
  44.  
  45. Public Overrides Function ToString() As String
  46. Dim Result As Decimal
  47. If Decimal.TryParse(_value, Result) = True Then
  48. 'Divide Result by (10 ^ DecimalPlaces) in order to get the amount of decimal places we want.'
  49. 'For example: 2 decimal places => Result / (10 ^ 2) = Result / 100 = x,xx.'
  50. Return Result / (10 ^ Me.DecimalPlaces).ToString("0." & New String("0"c, Me.DecimalPlaces))
  51. End If
  52. Return "<parse error>"
  53. End Function
  54.  
  55. Public Sub New(ByVal DecimalPlaces As Integer)
  56. If DecimalPlaces <= 0 Then DecimalPlaces = 1
  57. Me.DecimalPlaces = DecimalPlaces
  58. End Sub
  59. End Class
Success #stdin #stdout 0.02s 24448KB
stdin
Standard input is empty
stdout
Input: 415276
Result: 4152.76

RemoveRange(1, 3): 4.76