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

Surprising behavior of kendo.toString with one argument

1 Answer 159 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Mike
Top achievements
Rank 1
Mike asked on 02 May 2017, 02:42 PM

Code: kendo.toString(1.67)

Expected result: either "1.67" (as appropriate kiven the current kendo culture) or an error indicating that multiple arguments are required

Actual result: returns 1.67 as a number, not a string

It seems odd to me that the toString() function should ever return something other than a string.

___________________________

Context:

My use-case is that I am trying to format a numeric value in a culture-aware way using kendo.

In many cases, I can do this via kendo.toString, for example:

kendo.toString(1.67, "#.##")

However, in this case rather than specifying a custom format I wanted to use the "natural" format (as many decimal places as the number has). In C#, for example, you can get this by calling ToString() on a value of type double. I thought that kendo.toString(number) might have the same behavior, and was surprised when it returned a number back. A workaround for my use-case is to do kendo.toString(number, "#.############"), but the current behavior with one argument seems like a bug that might catch others up in the future.

1 Answer, 1 is accepted

Sort by
0
Tsvetina
Telerik team
answered on 04 May 2017, 09:13 AM
Hi Mike,

Indeed, this is the default behavior of the method when no second argument is specified. You can see its source code here:
https://github.com/telerik/kendo-ui-core/blob/master/src/kendo.core.js#L1046

However, not supplying a required parameter is an invalid usage and a correct result is not guaranteed in such a case. As you can see from the method signature in the API reference, only the third parameter is optional. 

You could log an issue in the Kendo UI repository for the development team to evaluate, even though changing the behavior of such a widely used method might be declined to avoid breaking existing logic.

Regards,
Tsvetina
Telerik by Progress
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
General Discussions
Asked by
Mike
Top achievements
Rank 1
Answers by
Tsvetina
Telerik team
Share this question
or