Results 1 to 14 of 14

Thread: how to show a string value in a double variable

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2024
    Posts
    66

    how to show a string value in a double variable

    Hello,

    I have the following code:
    sdm.SWRBerechnung.Zins = 3.5

    Zins is as double but i want that from this variable 3.5 % would be shown

    how to do this?

    thank you

  2. #2
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,776

    Re: how to show a string value in a double variable

    If you step through this code in the debugger you'll get some ideas. The snippet contains a link to format specifiers.

    Code:
            Dim doubleValue As Double = 3.5#
            ' https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings#standard-format-specifiers
    
            Dim s As String = doubleValue.ToString
    
            s = doubleValue.ToString("P1")
    
            s = (doubleValue / 100).ToString("P1")
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2024
    Posts
    66

    Re: how to show a string value in a double variable

    Hello,

    thank you but how can i show a double wert with percent in a double variable?
    can you also give an exmaple here please?

  4. #4
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,525

    Re: how to show a string value in a double variable

    Quote Originally Posted by mred View Post
    thank you but how can i show a double wert with percent in a double variable?
    You don't. A double is a numeric float value. Period.
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jan 2024
    Posts
    66

    Re: how to show a string value in a double variable

    for this exmaple i reciev cannot implicit conversion from string to integer.

    Dim s = (rs.Zins / 100).ToString("P1")

    I don't know wehre the problem is?

  6. #6
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,809

    Re: how to show a string value in a double variable

    In your example from post 1, you use:
    Code:
    sdm.SWRBerechnung.Zins = 3.5
    In your example from post 5, you use:
    Code:
    rs.Zins
    These are two separate variables. My guess without seeing any additional code is that rs.Zins is defined as a String.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  7. #7
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,525

    Re: how to show a string value in a double variable

    Quote Originally Posted by mred View Post
    for this exmaple i reciev cannot implicit conversion from string to integer.

    Dim s = (rs.Zins / 100).ToString("P1")

    I don't know wehre the problem is?
    What Type is rs.Zins?
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jan 2024
    Posts
    66

    Re: how to show a string value in a double variable

    no rs.zins is defined as double

  9. #9
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,809

    Re: how to show a string value in a double variable

    Are you sure you are getting that error on the line you provided us with?
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Jan 2024
    Posts
    66

    Re: how to show a string value in a double variable

    yes i get the error in this line
    Dim s = (rs.Zins / 100).ToString("P1")
    rs.zins is as double. i don't know why i recieve this message? under P1 is also red line.

    I recieve this message:
    Error BC30512 Option Strict On disallows implicit conversions from 'String' to 'Integer'.

  11. #11
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,525

    Re: how to show a string value in a double variable

    CultureInfo missing as second argument in ToString?
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  12. #12
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,157

    Re: how to show a string value in a double variable

    You may be sure rs.Zins is defined as double, but it sure sounds like the compiler believes otherwise. If you haven't done so already, you might try highlighting the rs.Zins part of the line in question and pressing Shift+F9. What data type does the compiler think that resolves to?

    We may be misunderstanding the situation. Since you didn't explicitly declare a type for s, removing the .ToString() should simplify the situation. Ultimately, doing so would be wrong, but for studying the problem, it makes it simpler.
    My usual boring signature: Nothing

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,474

    Re: how to show a string value in a double variable

    I just wrote this code:
    Code:
    Dim rsZins As Double = 12.5
    Dim s = (rsZins / 100).ToString("P1")
    It compiled without issue with Option Strict On and, when I ran it, s contained "12.5%". You're doing something wrong but you haven't provided us with enough information or, possibly, with accurate information in order to determine what that is.

  14. #14
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,474

    Re: how to show a string value in a double variable

    I can tell you what that error message means but not why you're getting it. The compiler things that the result of (rsZins / 100) is a String and there is no String.ToString overload that takes a String as an argument. The compiler is assuming that you are calling ToString with no arguments and thus not including the parentheses on the call, then you're trying to index the resulting String, but that indexing requires an Integer rather than a String argument. It thinks you want to do something like this:
    Code:
    Dim s = (rs.Zins / 100).ToString()(0)
    That would call ToString with no arguments, then get the Char at index zero from that String. That seems to be what the compiler thinks is happening but why it thinks that is a mystery, based on the information you've provided. You need to show us a more complete example that demonstrates the behaviour that you're seeing, so that we can see it for ourselves and determine the root cause.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width