Hitachi Vantara Pentaho Community Forums
Results 1 to 3 of 3

Thread: How to truncate a number using javascript?

  1. #1
    Join Date
    Jul 2007
    Posts
    247

    Default How to truncate a number using javascript?

    Hi everybody,

    this might be a basic question but somehow I don't seem to get it.

    I have a field containing a number, let's say e.g. 12345. I want to divide it by 100 and truncate it to get an integer value.

    My approach was:
    Code:
     FIELD2.setValue(FIELD1.getInteger()/100);
    but this doesn't work. Is there another way to get the result as an integer?

    As a workaround I used
    Code:
     FIELD2.setValue(floor(FIELD1.getInteger()/100));
    but this doesn't looks like an elegant way to me.

    Any suggestions?


    Regards,
    Ben

  2. #2
    Join Date
    May 2006
    Posts
    4,882

    Default

    mmmm ... what's not elegant about the floor?

    Regards,
    Sven

  3. #3
    Join Date
    Nov 2006
    Posts
    135

    Default Rounding or truncating?

    Dividing your integer value by 100 to truncate is a very reasonable thing to do. Turning the resulting value into an integer can be accomplished with any of the following:

    Math.floor(FIELD1), or Math.ceil(FIELD1), depending on how you want to truncate.

    If you want to round, you can use:

    Math.round(FIELD1);

    Or you could get real ugly, and convert it to a string and remove the last 2 characters with something like:

    var str = "" + FIELD1;
    var str = str.substring(0, str.length-2);
    FIELD1 = parseInt( str );
    Last edited by sbarkdull; 09-20-2007 at 04:07 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Privacy Policy | Legal Notices | Safe Harbor Privacy Policy

Copyright © 2005 - 2019 Hitachi Vantara Corporation. All Rights Reserved.