Integer and Decimal Parts

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Si

    Integer and Decimal Parts

    Hi.

    After the prompt reply to my message yeasterday (Problem with Opera an IE),
    I have another newbie question.

    How do I separate the integer and decimal part of a number, ie. If I had
    123456, and divided it by 1000 = 123.456,

    how do I obtain 123 and 456 as seperate variables?

    This is the sort of thing that is done using the "int" and "mod" function
    in Excel

    Cheers Si,
  • Lasse Reichstein Nielsen

    #2
    Re: Integer and Decimal Parts

    Si <s.e.r@cwcom.ne t> writes:
    [color=blue]
    > How do I separate the integer and decimal part of a number, ie. If I
    > had 123456, and divided it by 1000 = 123.456,[/color]
    [color=blue]
    > how do I obtain 123 and 456 as seperate variables?
    >
    > This is the sort of thing that is done using the "int" and "mod"
    > function in Excel[/color]

    Just use Math.floor and % (modulus operator).

    var intPart = Math.floor(1234 56 / 1000);
    var fracPart = 123456 % 1000;

    /L
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • Si

      #3
      Re: Integer and Decimal Parts

      Great Thanks!

      Si,

      On 01 Nov 2003 19:54:32 +0100, Lasse Reichstein Nielsen
      <lrn@hotpop.com > wrote:
      [color=blue]
      >Si <s.e.r@cwcom.ne t> writes:
      >[color=green]
      >> How do I separate the integer and decimal part of a number, ie. If I
      >> had 123456, and divided it by 1000 = 123.456,[/color]
      >[color=green]
      >> how do I obtain 123 and 456 as seperate variables?
      >>
      >> This is the sort of thing that is done using the "int" and "mod"
      >> function in Excel[/color]
      >
      >Just use Math.floor and % (modulus operator).
      >
      > var intPart = Math.floor(1234 56 / 1000);
      > var fracPart = 123456 % 1000;
      >
      >/L[/color]

      Comment

      • Si

        #4
        Re: Integer and Decimal Parts

        Thats Great!

        Thanks.

        Si,

        On 01 Nov 2003 19:54:32 +0100, Lasse Reichstein Nielsen
        <lrn@hotpop.com > wrote:
        [color=blue]
        >Si <s.e.r@cwcom.ne t> writes:
        >[color=green]
        >> How do I separate the integer and decimal part of a number, ie. If I
        >> had 123456, and divided it by 1000 = 123.456,[/color]
        >[color=green]
        >> how do I obtain 123 and 456 as seperate variables?
        >>
        >> This is the sort of thing that is done using the "int" and "mod"
        >> function in Excel[/color]
        >
        >Just use Math.floor and % (modulus operator).
        >
        > var intPart = Math.floor(1234 56 / 1000);
        > var fracPart = 123456 % 1000;
        >
        >/L[/color]

        Comment

        • Dr John Stockton

          #5
          Re: Integer and Decimal Parts

          JRS: In article <oprxy2fxznzqdx 0q@news.eclipse .co.uk>, seen in
          news:comp.lang. javascript, Si <s.e.r@cwcom.ne t> posted at Sat, 1 Nov
          2003 18:46:35 :-[color=blue]
          >Hi.
          >
          >After the prompt reply to my message yeasterday (Problem with Opera an IE),
          >I have another newbie question.
          >
          >How do I separate the integer and decimal part of a number, ie. If I had
          >123456, and divided it by 1000 = 123.456,
          >
          >how do I obtain 123 and 456 as seperate variables?[/color]

          Question : do you want to start with the 123456, or the 123.456? Should
          the results be numbers or strings? If the initial number had been
          123450, would you want 45 or 450 for the second result? If it had been
          123007, would you want 7 or 007?

          It is always best to define the problem completely in words, illustrated
          by salient examples.

          Note that 123.456 cannot be exactly represented as a Number; it is not
          easy to be sure that multiplying such by 1000 will always give a whole
          number. If it ever gives less than the whole number, then Math.floor()
          should give the next lower number.

          Number 123 comes from Math.floor(123. 456)
          Number 456 comes from Math.round(123. 456%1*1000)
          123.456*1000%10 00

          If the original number 123456 is known to be integer, it would be better
          to start with that and do only exact operations. If it is actually
          already available as string, use string operations.

          OK = /^(\d+)(\d\d\d)$/.test(123456) // true
          AnS = [RegExp.$1, RegExp.$2] // '123','456'
          AnN = [+RegExp.$1, +RegExp.$2] // 123,456

          One can. of course, define Int and Mod functions, for convenience of
          expression.

          --
          © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
          <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
          <URL:http://www.merlyn.demo n.co.uk/js-index.htm> JS maths, dates, sources.
          <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.

          Comment

          Working...