Rounding with NumericTextBox

8 posts, 0 answers
  1. Mohamed
    Mohamed avatar
    13 posts
    Member since:
    May 2015

    Posted 14 Mar Link to this post

    Greetings,

    I'm using the Numeric Text Box and I want to limit the number of decimal the user is allowed to enter to a certain value, Let's say 3

    I'm seeing the decimals property to 3 and the format to n3

    The display is working fine but the user is allowed to input 1.999999 for example and it will be rounded to 2.000

    I want it to be 1.999, either by limiting the input or, what I tried to do is forcing the value to be 1.999.

    I tried using the onchange event but when the event is fired, the value is already rounded to 2.

     

    Any help on this, 

    Thanks,

  2. Alexander Valchev
    Admin
    Alexander Valchev avatar
    2875 posts

    Posted 16 Mar Link to this post

    Hi Mohamed,

    You have to specify the precision, number of decimals.
    Here is a small example: http://jsbin.com/tojafiviya/edit?html,css,js,output

    Regards,
    Alexander Valchev
    Telerik
     
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
     
  3. Kendo UI is VS 2017 Ready
  4. Mohamed
    Mohamed avatar
    13 posts
    Member since:
    May 2015

    Posted 16 Mar in reply to Alexander Valchev Link to this post

    Hi Alexander,

    Thanks for your reply, but this doesn't resolve my problem as I'm already specifying the precision.

    in your example you set the decimals to 16, in my case I need it to be three. Here is how you can replicate my issue withing your example:

    change the decimals to be 3.

    Input the following value : 1.9999 (notice that you can put a number with decimals exceeding 3)

    Tab out, the value will change to 2.000; this is my issue. I need it to be 1.999 either by only allowing 3 decimals in the input or by disabling the rounding.

    I tried use the following function for the onchange event, but it didn't work as the value was already rounded to 2.000 when the event is fired.

    function displayMyNumberProperly(e) {
        var mult = Math.pow(10, this.options.decimals);
        if (this.value() > 0) {
            this.value(Math.floor(this.value() * mult) / mult)
        } else {
            this.value(Math.ceil(this.value() * mult) / mult)
        }
    };

     

  5. Alexander Valchev
    Admin
    Alexander Valchev avatar
    2875 posts

    Posted 18 Mar Link to this post

    Hi Mohamed,

    The rounding cannot be turned off completely. The best we can offer is to set a precision.
    Please check this forum thread for more information.

    Regards,
    Alexander Valchev
    Telerik
     
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
     
  6. Mohamed
    Mohamed avatar
    13 posts
    Member since:
    May 2015

    Posted 18 Mar in reply to Alexander Valchev Link to this post

    Thanks Alexander,

    Setting the precision to 16 and using my onchange function, with small changes to extract the real decimal value from the format,gave me what I'm looking for for now.

    I hope we will be able to limit the number of decimals digits the user can input in the future.

     

     

  7. thuy
    thuy avatar
    14 posts
    Member since:
    Nov 2013

    Posted 11 May Link to this post

    Hello Alex,

    I faced the same issue when  trying to setup a control like this. The "maxRate" has been defined by very big number (> 16 digits), but the control auto rounded for ex: 9999999999999999.00 =>10000000000000000.00, or 1111111111111111.00 => 11111111111111112.00, and the number is too long, it's became : 1.23e+20 (~1.23 and 20 digits 0). Do you have any update ?

    $("#controlId").kendoNumericTextBox({
        min: 0.01,
        max: maxRate,
        format: "n2",
        step: 0.01
    });

  8. Georgi Krustev
    Admin
    Georgi Krustev avatar
    3706 posts

    Posted 13 May Link to this post

    Hello thuy,

    In general, JavaScript Number object loses precision after the 16th digit and we cannot do much here. I am afraid that really big numbers  cannot be used in NumericTextBox. You can find a detailed information on the case here:
    Regards,
    Georgi Krustev
    Telerik
     
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
     
  9. thuy
    thuy avatar
    14 posts
    Member since:
    Nov 2013

    Posted 15 May in reply to Georgi Krustev Link to this post

    Thank you Georgi, it's help me alot.

    Best Regards,

Back to Top
Kendo UI is VS 2017 Ready