Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. What a gem.

What a gem.

Scheduled Pinned Locked Moved The Weird and The Wonderful
rubycom
37 Posts 27 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Mark AJA

    Don't know what language this is, but I'm sure you could use the following to reduce the code length:

    ' ...
    Return (SystemInformation.PowerStatus.BatteryLifePercent.ToString *100) & "%"
    ' . End of function.

    P Offline
    P Offline
    Paladin2000
    wrote on last edited by
    #28

    Actually, it's:

    Return (SystemInformation.PowerStatus.BatteryLifePercent * 100).ToString() + "%"

    You did the ToString before the multiplication. Note that this:

    SystemInformation.PowerStatus.BatteryLifePercent.ToString("p")

    ...does create a percentage conversion, but since it has decimals and a space it is not equivalent.

    M B 2 Replies Last reply
    0
    • P Paladin2000

      Actually, it's:

      Return (SystemInformation.PowerStatus.BatteryLifePercent * 100).ToString() + "%"

      You did the ToString before the multiplication. Note that this:

      SystemInformation.PowerStatus.BatteryLifePercent.ToString("p")

      ...does create a percentage conversion, but since it has decimals and a space it is not equivalent.

      M Offline
      M Offline
      Mark AJA
      wrote on last edited by
      #29

      I used & and not + as & can join a string and a number. + will only add two nunbers or attach two strings. I would usually use CStr$() but as I was not to sure what language this was, used & as it's used in a number of languages.

      P 1 Reply Last reply
      0
      • J Julien Villers

        It couldn't have been done with a generic approach, look at the last line:

        Else
        Return "NA"

        That required a custom solution! Just kidding ;) BTW, shouldn't that be 'N/A'?

        'As programmers go, I'm fairly social. Which still means I'm a borderline sociopath by normal standards.' Jeff Atwood 'I'm French! Why do you think I've got this outrrrrageous accent?' Monty Python and the Holy Grail

        G Offline
        G Offline
        GBSC
        wrote on last edited by
        #30

        NA is correct because if you can't determine the battery percentage you must be dealing with sodium :)

        1 Reply Last reply
        0
        • F Fabio Franco

          Wow! This is only not worse than what I've seen, because the DB field length related to what I found is small:

          public string GetStringToDatabase(int value)
          {
          if (value > 99999999)
          return "0" + value.ToString();
          else if (value > 9999999)
          return "00" + value.ToString();
          else if (value > 999999)
          return "000" + value.ToString();
          .
          .
          .
          }

          "To alcohol! The cause of, and solution to, all of life's problems" - Homer Simpson

          J Offline
          J Offline
          James Lonero
          wrote on last edited by
          #31

          I did something like this when I was programming in assembly language back in college. I was making a way to get a value out of a register out to the terminal in an integer form. Then, we only had 16 bit registers and could only output -32738 to 32737. But, there was no library routines to output to the terminal.

          F 1 Reply Last reply
          0
          • J James Lonero

            I did something like this when I was programming in assembly language back in college. I was making a way to get a value out of a register out to the terminal in an integer form. Then, we only had 16 bit registers and could only output -32738 to 32737. But, there was no library routines to output to the terminal.

            F Offline
            F Offline
            Fabio Franco
            wrote on last edited by
            #32

            Well, although in assembly one can develop a routine for that, it's forgivable if you don't, specially in college while learning. What's unforgivable is having this in a high level language in production code for a mission critical application.

            "To alcohol! The cause of, and solution to, all of life's problems" - Homer Simpson

            1 Reply Last reply
            0
            • M Mark AJA

              I used & and not + as & can join a string and a number. + will only add two nunbers or attach two strings. I would usually use CStr$() but as I was not to sure what language this was, used & as it's used in a number of languages.

              P Offline
              P Offline
              Paladin2000
              wrote on last edited by
              #33

              The "+" operator does work here, it implicitly converts the number to a string and concatenates it to the other. But yes, the "&" operator is for concatenation explicitly. However, in your example, my key point was that you used the "*" operator between a number and a string...

              M 1 Reply Last reply
              0
              • P Paladin2000

                The "+" operator does work here, it implicitly converts the number to a string and concatenates it to the other. But yes, the "&" operator is for concatenation explicitly. However, in your example, my key point was that you used the "*" operator between a number and a string...

                M Offline
                M Offline
                Mark AJA
                wrote on last edited by
                #34

                I don't know what language uses a * between two strings. * is usually used to multiply two numbers. EG 123*4 = 492 + is used to add two numbers. EG 123+4 = 127. 123+"4" = error & is used to join two numbers, strings or a number and a string. EG 123 & 4 = "1234" If a language returns 492 or "492" from 123*"4" then in my opinion the language needs an update. The following Visual Basic 5 code:

                Private Sub Form_Load()
                Dim a As Integer ' or as Long or Double
                Dim b As String
                a = 123
                b = "4"
                MsgBox a * b
                End Sub

                displays 492, but should display an error acording to other versions of Basic.

                1 Reply Last reply
                0
                • P Paladin2000

                  Actually, it's:

                  Return (SystemInformation.PowerStatus.BatteryLifePercent * 100).ToString() + "%"

                  You did the ToString before the multiplication. Note that this:

                  SystemInformation.PowerStatus.BatteryLifePercent.ToString("p")

                  ...does create a percentage conversion, but since it has decimals and a space it is not equivalent.

                  B Offline
                  B Offline
                  bbirajdar
                  wrote on last edited by
                  #35

                  excellent observation

                  1 Reply Last reply
                  0
                  • A AaronM_NZ

                    Stumbled across this gem today. http://pastebin.com/4Nx8yggU[^] For preservation sake, here is a snippet, but you get the idea:

                    Public ReadOnly Property BatteryPercent()
                    ' This code will retrieve the BatteryLifePercent property and convert it to a percent.
                    Get
                    If SystemInformation.PowerStatus.BatteryLifePercent.ToString = "1" Then
                    Return "100%"
                    ElseIf SystemInformation.PowerStatus.BatteryLifePercent.ToString = "0.99" Then
                    Return "99%"
                    ElseIf SystemInformation.PowerStatus.BatteryLifePercent.ToString = "0.98" Then
                    Return "98%"
                    ElseIf SystemInformation.PowerStatus.BatteryLifePercent.ToString = "0.97" Then
                    Return "97%"
                    ... etc

                    Wow is all I can say.

                    M Offline
                    M Offline
                    Mohibur Rashid
                    wrote on last edited by
                    #36

                    Well, I can see he has time to do code :))))))))))

                    1 Reply Last reply
                    0
                    • A AaronM_NZ

                      Stumbled across this gem today. http://pastebin.com/4Nx8yggU[^] For preservation sake, here is a snippet, but you get the idea:

                      Public ReadOnly Property BatteryPercent()
                      ' This code will retrieve the BatteryLifePercent property and convert it to a percent.
                      Get
                      If SystemInformation.PowerStatus.BatteryLifePercent.ToString = "1" Then
                      Return "100%"
                      ElseIf SystemInformation.PowerStatus.BatteryLifePercent.ToString = "0.99" Then
                      Return "99%"
                      ElseIf SystemInformation.PowerStatus.BatteryLifePercent.ToString = "0.98" Then
                      Return "98%"
                      ElseIf SystemInformation.PowerStatus.BatteryLifePercent.ToString = "0.97" Then
                      Return "97%"
                      ... etc

                      Wow is all I can say.

                      K Offline
                      K Offline
                      krumia
                      wrote on last edited by
                      #37

                      Quote:

                      ' This code will retrieve the BatteryLifePercent property and convert it to a percent.

                      Really? Why would someone convert something to percent which is already in percent?

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups