Problem with CStr in VB .net

Hello!
I have a question about using CSTr.Everytime I do it in my decimal number
it adds 0 at the decimal part.
How can I make it get the true value( no excess zeros) of my number

Thanks guys!




Answer this question

Problem with CStr in VB .net

  • Suprotim Agarwal

    I'm not sure I fully understand you question which means I may not be answering the correction question.

    IF what you are saying that you are not happy with a displayed output, lookat the Format statement. You'll like it!

  • JT Gartner

    Hello!
    Thanks for your code really appreciate it.

  • SuperM

    The following code should produce the same results for you.



    Public Class Form1

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim s_string As String
            Dim int1 As Integer
            Dim int2 As Integer
            Dim int3 As Integer
            Dim int4 As Integer

            s_string = "1.2"
            int1 = Math.Round(CSng(s_string), 0)   'produces value of 1
            int2 = CInt(s_string)   'produces value of 1
            int3 = Val(s_string)  'Produces value of 1

            s_string = "5"
            int1 = Math.Round(CSng(s_string), 0)   'produces value of 5
            int2 = CInt(s_string)   'produces value of 5
            int3 = Val(s_string)  'Produces value of 5

            int4 = Integer.Parse("4.3", New System.Globalization.CultureInfo("En-us")) 'Should produce 4
        End Sub
    End Class

     


    If the first 3 integers don't work, but int4 works, then it's probably your current culture settings.
    if it's only a few specific lines, then i'd suggest using the technique used with int4.
    You can set a application global culture setting aswell.



    System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture

     



    Dustin.



  • Aaron Oneal

    Thanks for your reply.
    Sorry for the vague question.
    For example I have a number
     num = 12.32 (from Oracle database with Number (12,3) type)
    And I'm doing
     str = CStr(num)
    It outputs 12.320

    And when I have
     num = 12.1
     str = CStr(num)
    It outputs str = 12.100

    What I really want Is to output the number as is(in the first example as
    12.32 and at the sec as 12.1).I really don't want to set the decimal
    places(which I think Format can do) just the plain number(another vauge
    concept...) itself..

    Thanks!


  • Problem with CStr in VB .net