This is a migrated thread and some comments may be shown as answers.

RadNumeric TextBox Negative Rounding

5 Answers 105 Views
Input
This is a migrated thread and some comments may be shown as answers.
Jeff
Top achievements
Rank 1
Jeff asked on 07 Jan 2010, 05:14 PM

I have a form with a RadNumeric Textbox on it and here are the following results…

 

34.5 = 35 (rounding away from zero)

34.6 = 35 (rounding away from zero)

 

**** -34.5 = -34 (rounding closer to zero) ****

-34.6 = -35 (rounding away from zero)

 

The question I have, is why does .5 round closer to zero when it is a negative number?  Is there a way to achieve the expected results…

 

34.5 = 35 (rounding away from zero)

34.6 = 35 (rounding away from zero)

 

****  -34.5 = -35 (rounding away from zero) ****

-34.6 = -35 (rounding away from zero)

Thanks,
Steve

5 Answers, 1 is accepted

Sort by
0
Dimo
Telerik team
answered on 08 Jan 2010, 11:03 AM
Hello Tim,

The described behavior will be observed if you set AllowRounding="false" and DecimalDigits="0". Please set AllowRounding="true" (or remove the property) and -34.5 will be transformed to -35 upon blur.

Regards,
Dimo
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Jeff
Top achievements
Rank 1
answered on 08 Jan 2010, 04:03 PM

The AllowRounding is set to “true” and the DecimalDigits is “0” and -34.5 is rounding to -34 not -35.

 

The results that I am getting above in the first post are happening with the following values…

AllowRounding = true

CultureNativeDecimalSeparator = “.”

DecimalDigits = 0

DecimalSeparator = “.”

GroupSeparator = “,”

GroupSizes = 3

KeepNotRoundedValue = true

NegativePattern = “-n”

PositivePattern = “n”

 

Are there any other ideas on what to do or should I create a ticket?

 

Thanks,

Steve

0
Dimo
Telerik team
answered on 08 Jan 2010, 04:12 PM
Hello,

I am not able to reproduce the problem with the specified list of properties and values. The RadNumericTextBox rounds -34.5 to -35.

Please send a runnable demo.

Kind regards,
Dimo
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Jeff
Top achievements
Rank 1
answered on 08 Jan 2010, 09:21 PM
Created a ticket that has the demo attached.  Attached a screen shot to this forum showing the results from the demo project you requested.
Ticket #271898
Thanks,
Steve Fidler

0
Dimo
Telerik team
answered on 11 Jan 2010, 09:17 AM
Hello Tim,

Thank you for the runnable demo. I tested with a couple of versions and actually we have fixed the issue for Q2 2009 SP1 (2009.2.826). Please upgrade.

An alternative option is to include or register the following Javascript in the <body> of the web page, so that it overrides the method from the embedded resource script file. You can remove it after upgrading in the future.

if (typeof(Telerik) != "undefined" && typeof(Telerik.Web.UI.NumberFormat) != "undefined")
{
    Telerik.Web.UI.NumberFormat.Round = function(number, config)
    {
        if (!config.get_numberFormat().AllowRounding)
        {
            var decDig = config.get_numberFormat().DecimalDigits;
            if (decDig == 0 && number.toString().indexOf('.') != -1)
            {
                return number.toString().substr(0, number.toString().indexOf('.') + 1);
            }
            else if (decDig > 0 && number.toString().indexOf('.') != -1)
            {
                var parts = number.toString().split('.');
                if (parts[1].length > decDig)
                {
                    return parseFloat(parts[0] + '.' + parts[1].substr(0, decDig));
                }
                else
                {
                    return number;
                }
            }
            else
            {
                return number;
            }
        }
 
        //Very large and very small numbers are converted to scientific notation
        //in textual representation, therefore we degrade to the less precise
        //method of rounding
        if (number.toString().indexOf('e') > -1)
        {
            var pow = Math.pow(10, config.get_numberFormat().DecimalDigits);
            return Math.round(number * pow) / pow;
        }
        else if (number.toString().indexOf('.') > -1)
        {
 
            var parts = number.toString().split('.');
            var integralPart = Math.abs(parseInt(parts[0]));    //extract the integral part
            var decimalPart = parseFloat("0." + parts[1]);      //extract the decimal part
            var decimalPartLength = parts[1].length;
            var sign = parts[0].charAt(0) == '-' ? -1 : 1;      //save the sign
 
            //Return the number itself, if its decimal digits count is equal to
            //the DecimalDigits setting in the NumberFormat
            if (parts[1].length == config.get_numberFormat().DecimalDigits)
            {
                return number;
            }
 
            //raise the decimal part to power of 10 needed to make it integer
            var temp = decimalPart * Math.pow(10, decimalPartLength);
 
            //delete by 10 and round until we leave with the specified
            //number of decimal digits
            for (var i = 1; i <= decimalPartLength; i++)
            {
                if (i <= decimalPartLength - config.get_numberFormat().DecimalDigits)
                {
                    temp = Math.round(temp / 10);
                }
                else
                {
                    temp /= 10;
                }
            }
 
            /*
            Handles cases when the decimal part if of the form: 0.00546 and we want to
            round to 3 decimal digits. At this point, temp would be 0.546 and we still
            need to divide twice by ten to get 0.005. The following IF statement
            handles cases when the decimal needs to be adjusted in such way
            */
            if (temp.toString().indexOf('.') > -1)
            {
 
                var fracLengthAfterRounding = temp.toString().split('.')[1].length;
                if (fracLengthAfterRounding > config.get_numberFormat().DecimalDigits)
                {
                    var pow = Math.pow(10, config.get_numberFormat().DecimalDigits);
                    temp = Math.round(temp * pow) / pow;
                }
            }
             
            var preliminaryResult = sign * (integralPart + temp);
            if (config.get_numberFormat().DecimalDigits > 0 &&
                preliminaryResult.toString().indexOf(".") != -1 &&
                preliminaryResult.toString().split(".")[1].length > config.get_numberFormat().DecimalDigits)
            {
                return sign * (parseFloat(integralPart + "." + temp.toString().substr(2)));
            }
            else
            {
                return preliminaryResult;
            }
        }
        else
        {
            return number;
        }
    }
}


Kind regards,
Dimo
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
Input
Asked by
Jeff
Top achievements
Rank 1
Answers by
Dimo
Telerik team
Jeff
Top achievements
Rank 1
Share this question
or