How would I get the number after a decimal point in VB.NET

后端 未结 8 1915
猫巷女王i
猫巷女王i 2021-01-27 12:10

Can anyone give me an easy way to find out the numbers after the decimal point of the double?
All i need to do is to find out if the number ends in 0 like 1.0 or 10.0 or 32

相关标签:
8条回答
  • 2021-01-27 12:59

    If your input is a string, and you want to identify strings that have decimals, but where those decimal places are all zero, then you could use a regular expression:

    var regex = new Regex("^-?\\d+\\.0+$");
    if (new Regex.IsMatch(input))
    {
        // it's in the format you want
    }
    
    0 讨论(0)
  • 2021-01-27 13:03

    It's been a while, nevertheless my solution:

    I also used the mod function, but you have to be careful with that as there are precision "problems" with the data type 'Double':

    Dim numberDbl As Double = 1.2
    Dim modValDbl As Double = 1.0
    Dim remValDbl As Double = numberDbl Mod modValDbl
    'remValDbl = 0.19999999999999996 -> not 100% precise
    
    Dim numberDec As Decimal = 1.2D
    Dim modValDec As Decimal = 1D
    Dim remValDec As Decimal = numberDec Mod modValDec
    'remValDec = 0.2 -> correct
    

    Therefore I wrote this function: (example: number = 1.25)

    • I first take the parameter number and mod it with its own integer (1.25 mod 1)
    • so I get the decimal value of my number and store it to remVal = 0.25
    • in the loop I keep multiplying remVal with 10 and compare this value with the same truncated value (i.e.: 2.5 <> 2) until they are the same
    • with every loop I increment the multiplier by 10
    • as soon the two values match (i.e.: 25 = 25) I can return my decimal value using remVal and multiplier

      Public Shared Function getDecimalPlaces(number As Double) As Double
      Dim modVal As Integer = CInt(If(Math.Truncate(number) = 0, 1, Math.Truncate(number)))
      Dim remVal As Decimal = CDec(number) Mod modVal
      Dim retVal As Double = 0
      
      Debug.WriteLine("modVal: " + modVal.ToString)
      Debug.WriteLine("remVal: " + remVal.ToString)
      Dim multiplier As Decimal = 1
      
      While remVal * multiplier <> Math.Truncate(remVal * multiplier)
          Debug.WriteLine("remVal * multiplier <> Math.Truncate(remVal * multiplier): " + (remVal * multiplier).ToString + " <> " + (Math.Truncate(remVal * multiplier)).ToString)
          multiplier *= 10
          Debug.WriteLine("remVal * multiplier <> Math.Truncate(remVal * multiplier): " + (remVal * multiplier).ToString + " <> " + (Math.Truncate(remVal * multiplier)).ToString)
      End While
      
      retVal = CDbl(remVal * multiplier)
      
      Debug.WriteLine("remVal: " + remVal.ToString)
      Debug.WriteLine("multiplier: " + multiplier.ToString)
      Debug.WriteLine("retVal: " + retVal.ToString)
      Return retVal
      End Function
      
      'Output with number = 1.25
      'modVal: 1
      'remVal: 0,25
      'remVal * multiplier <> Math.Truncate(remVal * multiplier): 0,25 <> 0 -> true
      'remVal * multiplier <> Math.Truncate(remVal * multiplier): 2,50 <> 2 -> true
      'remVal * multiplier <> Math.Truncate(remVal * multiplier): 2,50 <> 2 -> true
      'remVal * multiplier <> Math.Truncate(remVal * multiplier): 25,00 <> 25 -> false: exit while
      'remVal: 0,25
      'multiplier: 100
      'retVal: 25
      
    0 讨论(0)
提交回复
热议问题