Two Decimal Point For Double Value

How to get two decimal point value for a double value For example 231.321312321312132 = 231.32 and 231 = 231.00. Thank you.

Answer this question

Two Decimal Point For Double Value

  • shakalama

    Hi,

    Quick eg of a bit of code that might be used to always roundup / down a value to the relevant x00000 value.

    Dim num As Double = 3238846823424.4565

    Dim numLen As Integer = num.ToString.Length

    Dim decPos As Integer = num.ToString.IndexOf("."c)

    Dim divider As Double

    If decPos >= 0 Then

    divider = CType(String.Concat("1", New String("0"c, numLen - decPos - 1)), Double)

    Else : divider = CType(String.Concat("1", New String("0"c, numLen - 1)), Double)

    End If

    '' Round up or down using Ceiling/Floor method and then multiply by the divider

    Dim value As Double = Math.Floor(num / divider) * divider

    I haven't exactly checked it but I gave it a quick run an it seemed to churn out the value you are seeking.

    Richard


  • R Raghu

    Hi

    It depends on what type of rounding you want (eg Bankers rounding).

    Take a look at these methods, you'll probably find what you need.

    Math.Round ... as described already

    Math.Ceiling ... rounds up to the nearest integer value

    Math.Floor ... rounds down to the nearest integer value

    Hope this helps

    Richard

    Your zero counting question ..... doesn't seem to make much sense .... how would you want to interpret a number such as 334002877400001 ... would you expect the answer 6


  • Aleniko29139

    Dick Donny wrote:

    Hi

    It depends on what type of rounding you want (eg Bankers rounding).

    Take a look at these methods, you'll probably find what you need.

    Math.Round ... as described already

    Math.Ceiling ... rounds up to the nearest integer value

    Math.Floor ... rounds down to the nearest integer value

    Hope this helps

    Richard

    Your zero counting question ..... doesn't seem to make much sense .... how would you want to interpret a number such as 334002877400001 ... would you expect the answer 6

    Hi,

    No, sorry, as my code counts the length of the number as a string minus 1.

    It would produce.>> 14 from the number>> "334002877400001" as a string as it gets approximated to>>

    300000000000000

    As per my DISCLAIMER: 334002877400001 would still become 300000000000000

    even if it were changed to closer to 400000000000000 like 398765432101234

    Regards,

    S_DS



  • carp

     lucerias wrote:
    Is there any way to check how many zero(s) of an integer, for example 2000000 has 6 zero(s) I am thinking of using For Loop, is there any other way else
     
    Besides that, how to round the value from 3,242,423,432 to 4,000,000,000 Thank you.

    'DISCLAIMER: Note this code doesn't round up or down specifically.

    'It just takes the 1st number and adds the noughts!!

    Dim myValue As Double

    myValue = 3242423432.456789

    myValue = Int(myValue)

    Dim myLength, index As Integer

    Dim newString1, newString2 As String

    newstring1 = myValue.ToString

    myLength = newString1.Length

    MsgBox("Number of noughts is " & (myLength - 1).ToString)

    newString2 = newString1.Substring(0, 1)

    For index = 0 To myLength - 2

    newString2 = newString2 & "0"

    Next

    MsgBox(myValue.ToString)

    MsgBox("New number is " & newString2)

    End Sub

     

    Regards,

    S_DS

     



  • jbm007

    Is there any way to check how many zero(s) of an integer, for example 2000000 has 6 zero(s) I am thinking of using For Loop, is there any other way else
     
    Besides that, how to round the value from 3242423432 to 4000000000 Thank you.

  • zproxy

    myValue=Math.Round(myValue,2)

    Regards,

    S_DS



  • Two Decimal Point For Double Value