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. IT & Infrastructure
  4. Printing 0.346346346 as 0.(346)

Printing 0.346346346 as 0.(346)

Scheduled Pinned Locked Moved IT & Infrastructure
question
11 Posts 4 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.
  • A Offline
    A Offline
    Arun Immanuel
    wrote on last edited by
    #1

    Hi All, Can anyone please give me an idea to write a program(any language) to print 0.123123123 as 0.(123) and 0.346346346 as 0.(346) if both the numerator and denominator are given?

    Regards, Arun Kumar.A

    R L 2 Replies Last reply
    0
    • A Arun Immanuel

      Hi All, Can anyone please give me an idea to write a program(any language) to print 0.123123123 as 0.(123) and 0.346346346 as 0.(346) if both the numerator and denominator are given?

      Regards, Arun Kumar.A

      R Offline
      R Offline
      realJSOP
      wrote on last edited by
      #2

      Here's some C++ code:

      double value = 0.123123123;
      printf("%0.3lf", value);

      Her it is in C#:

      double nVal = 0.346789123;
      string sVal = string.Format("{0:#.000}", nVal);

      Here it is in VB:

      START:
      print("Not a real programming language")
      goto START

      "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
      -----
      "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

      A 1 Reply Last reply
      0
      • R realJSOP

        Here's some C++ code:

        double value = 0.123123123;
        printf("%0.3lf", value);

        Her it is in C#:

        double nVal = 0.346789123;
        string sVal = string.Format("{0:#.000}", nVal);

        Here it is in VB:

        START:
        print("Not a real programming language")
        goto START

        "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
        -----
        "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

        A Offline
        A Offline
        Arun Immanuel
        wrote on last edited by
        #3

        So you didn't understand the question properly. Have you noticed that repeating patterns are enclosed within parenthesis. For example: 0.565656 => 0.(56) 0.111111 => 0.(1) 0.123412341234=> 0.(1234)

        Regards, Arun Kumar.A

        R 1 Reply Last reply
        0
        • A Arun Immanuel

          So you didn't understand the question properly. Have you noticed that repeating patterns are enclosed within parenthesis. For example: 0.565656 => 0.(56) 0.111111 => 0.(1) 0.123412341234=> 0.(1234)

          Regards, Arun Kumar.A

          R Offline
          R Offline
          realJSOP
          wrote on last edited by
          #4

          If you pay me $500, I'll write you a function to do that in C++. If you pay me $1000, I'll do it in C#. If you pay me $10,000, I'll do it in VB. I don't work that hard for free.

          "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
          -----
          "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

          B A 2 Replies Last reply
          0
          • R realJSOP

            If you pay me $500, I'll write you a function to do that in C++. If you pay me $1000, I'll do it in C#. If you pay me $10,000, I'll do it in VB. I don't work that hard for free.

            "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
            -----
            "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

            B Offline
            B Offline
            Bassam Saoud
            wrote on last edited by
            #5

            John Simmons / outlaw programmer wrote:

            $10,000, I'll do it in VB.

            Are you sure? I mean its VB and I know your views on the subject :laugh:

            1 Reply Last reply
            0
            • R realJSOP

              If you pay me $500, I'll write you a function to do that in C++. If you pay me $1000, I'll do it in C#. If you pay me $10,000, I'll do it in VB. I don't work that hard for free.

              "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
              -----
              "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

              A Offline
              A Offline
              Arun Immanuel
              wrote on last edited by
              #6

              I didn't ask you to write the program.. Please read the original post.. I have asked just the idea.. Not the program..

              Regards, Arun Kumar.A

              1 Reply Last reply
              0
              • A Arun Immanuel

                Hi All, Can anyone please give me an idea to write a program(any language) to print 0.123123123 as 0.(123) and 0.346346346 as 0.(346) if both the numerator and denominator are given?

                Regards, Arun Kumar.A

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                Hi Arun, in your notation 0.(Z) with Z an arbitrary sequence of digits has the value Z/N where N consists of n times the digit 9, n being the length of sequence Z. So in order to have the equality X/Y = "0.(Z)" you must solve X/Y=Z/N or N=Z*Y/X for Z and n The easiest way to do this is using a loop where n grows from 1 to infinity, then try to evenly divide N (n digits 9) by Y The first solution, if any, is the one you want. Examples: X=1, Y=3 try n=1 N=9 solved ! found "0.(3)" X=1, Y=37 n=1? 9/37 no way n=2? 99/37 nope n=3? 999/37=27 found "0.(027)" The next interesting question is how do you know you tried a sufficiently long sequence of nines ? Clearly if X=1, Y=2 you will never find a solution ! :)

                Luc Pattyn [My Articles] [Forum Guidelines]

                A 1 Reply Last reply
                0
                • L Luc Pattyn

                  Hi Arun, in your notation 0.(Z) with Z an arbitrary sequence of digits has the value Z/N where N consists of n times the digit 9, n being the length of sequence Z. So in order to have the equality X/Y = "0.(Z)" you must solve X/Y=Z/N or N=Z*Y/X for Z and n The easiest way to do this is using a loop where n grows from 1 to infinity, then try to evenly divide N (n digits 9) by Y The first solution, if any, is the one you want. Examples: X=1, Y=3 try n=1 N=9 solved ! found "0.(3)" X=1, Y=37 n=1? 9/37 no way n=2? 99/37 nope n=3? 999/37=27 found "0.(027)" The next interesting question is how do you know you tried a sufficiently long sequence of nines ? Clearly if X=1, Y=2 you will never find a solution ! :)

                  Luc Pattyn [My Articles] [Forum Guidelines]

                  A Offline
                  A Offline
                  Arun Immanuel
                  wrote on last edited by
                  #8

                  Thank you very much. You have been most helpful.:):):)

                  Regards, Arun Kumar.A

                  L 1 Reply Last reply
                  0
                  • A Arun Immanuel

                    Thank you very much. You have been most helpful.:):):)

                    Regards, Arun Kumar.A

                    L Offline
                    L Offline
                    Luc Pattyn
                    wrote on last edited by
                    #9

                    You're welcome. I would suggest you pay extra attention to my final remark, since an algorithm that potentially takes forever is not really an algorithm (an algorithm is supposed to provide a solution in a finite time and at a reasonable cost). Some more math should lead you to a maximum for n, depending on X and Y. :)

                    Luc Pattyn [My Articles] [Forum Guidelines]

                    R 1 Reply Last reply
                    0
                    • L Luc Pattyn

                      You're welcome. I would suggest you pay extra attention to my final remark, since an algorithm that potentially takes forever is not really an algorithm (an algorithm is supposed to provide a solution in a finite time and at a reasonable cost). Some more math should lead you to a maximum for n, depending on X and Y. :)

                      Luc Pattyn [My Articles] [Forum Guidelines]

                      R Offline
                      R Offline
                      realJSOP
                      wrote on last edited by
                      #10

                      Luc Pattyn wrote:

                      an algorithm that potentially takes forever is not really an algorithm

                      Yeah - it's .Net...

                      "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                      -----
                      "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                      L 1 Reply Last reply
                      0
                      • R realJSOP

                        Luc Pattyn wrote:

                        an algorithm that potentially takes forever is not really an algorithm

                        Yeah - it's .Net...

                        "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                        -----
                        "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                        L Offline
                        L Offline
                        Luc Pattyn
                        wrote on last edited by
                        #11

                        I am afraid you missed the point of the question, as well as of the first part of the solution. Maybe you can come up with the second half of the solution: an effective loop termination condition. Please keep in mind that so far no programming language or framework has been specified, and numeric variables have whatever capacity is required... And before you object, yes this topic should have been posted in Algo&Math forum rather than here. :)

                        Luc Pattyn


                        try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


                        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