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. General Programming
  3. C#
  4. How to display # using / and %

How to display # using / and %

Scheduled Pinned Locked Moved C#
helptutorialcsharplearning
3 Posts 3 Posters 0 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.
  • P Offline
    P Offline
    programming101
    wrote on last edited by
    #1

    :)I am new to programming and just started learning C#. I am using Visual C# 2005. I am working on a project where the user enters 5 digit # and then I need the digits to display separated by a space. This is a console application. I got as far as asking to enter # then displaying the digits by using / and %. I am having a problem showing the 2nd, 3rd and 4th #. How do you get the just the 1st digit to the right of the decimal to show. I used for example: digit1 = ( value1 / 10000); digit2 = ( value1 % 10000); digit3 = ( value1 % 1000); digit4 = ( value1 % 100); digit5 = ( value1 % 10); I am having a problem with the 2nd, 3rd and 4th to display only the first digit to the right of the decimal. All have been declared int..any help is appreciated, thanks Thanks. Hope my post is okay with detail if not let me know, this is my first post

    C J 2 Replies Last reply
    0
    • P programming101

      :)I am new to programming and just started learning C#. I am using Visual C# 2005. I am working on a project where the user enters 5 digit # and then I need the digits to display separated by a space. This is a console application. I got as far as asking to enter # then displaying the digits by using / and %. I am having a problem showing the 2nd, 3rd and 4th #. How do you get the just the 1st digit to the right of the decimal to show. I used for example: digit1 = ( value1 / 10000); digit2 = ( value1 % 10000); digit3 = ( value1 % 1000); digit4 = ( value1 % 100); digit5 = ( value1 % 10); I am having a problem with the 2nd, 3rd and 4th to display only the first digit to the right of the decimal. All have been declared int..any help is appreciated, thanks Thanks. Hope my post is okay with detail if not let me know, this is my first post

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      Hi - welcome to Code Project. digit2 = value1 % 10000 will give you the remainder when dividing by 10000. In essence, this means it will give you the last four digits. To get just the one digit, you'd also have to strip to the right, so something like this should work Math.Floor((value1 % 10000)/1000) The mod (%) strips values to the left, then the divide strips to the right. Finally, Math.Floor stops a result such as 3.6 from rounding up. The other approach is to do this: string number = value1.ToString(); digit1 = int.Parse(number[0]); digit2 = int.Parse(number[1]); etc You don't need to use int.TryParse or char.IsDigit, as you know the values are all numbers. You DO have to make sure your number is 5 digits long, I'd be inclined to store the values in a list or an array, then you can write code that works with any length number.

      Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

      1 Reply Last reply
      0
      • P programming101

        :)I am new to programming and just started learning C#. I am using Visual C# 2005. I am working on a project where the user enters 5 digit # and then I need the digits to display separated by a space. This is a console application. I got as far as asking to enter # then displaying the digits by using / and %. I am having a problem showing the 2nd, 3rd and 4th #. How do you get the just the 1st digit to the right of the decimal to show. I used for example: digit1 = ( value1 / 10000); digit2 = ( value1 % 10000); digit3 = ( value1 % 1000); digit4 = ( value1 % 100); digit5 = ( value1 % 10); I am having a problem with the 2nd, 3rd and 4th to display only the first digit to the right of the decimal. All have been declared int..any help is appreciated, thanks Thanks. Hope my post is okay with detail if not let me know, this is my first post

        J Offline
        J Offline
        Julian Nicholls
        wrote on last edited by
        #3

        Another possibility is comething like this. Obviously this will destroy the input value, but if its needed it can be saved first. // value is the 5-digit input value int[] digit = new int[5](); for( int i = 4; i >= 0; --i ) { digit[i] = value % 10; value /= 10; } All the Best Julian N. -- modified at 9:09 Sunday 9th September, 2007

        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