1

i'm building app using kendo (v. 2019.2.514). And i have strange error with number format by NumericTextBox.

At this moment i'm creating a form to modify products. Problem is that, i'm getting different results for my values.

Values are stored in database as floats. In model as well. And i'm pretty sure that problem is with the format because when value is a natural number it's fine, but:

(first control format)
0.1 gives 1
0.05 gives 5

(2nd format)
1.2 gives 120000004768372 (which means the original value was multiplied by 1e+14)
0.3 gives 300000011920929 (* 1e+15)

that are my 2 kinds of NumericTextBox formats

@(Html.Kendo().NumericTextBox<float>().Name("Weight").Min(0).Value(Model.Weight).Format("#.## kg")
                    .Placeholder($"{Language.translate("Weight")} (kg)").HtmlAttributes(new { validate = true })
                )

@(Html.Kendo().NumericTextBox<float>().Name("upTo7").Min(0).Format("#.## €").Value(Model.Price)
                    .Placeholder(Language.translate("Price up to x days").Replace("x", "7")).HtmlAttributes(new { validate = true })
                )

I've already tried formats like #.00, n2 and nothing works for me

I just want to display these numbers properly

Jarzyn
  • 19
  • 8

2 Answers2

0

The problem seems to be in with your Model because I just tried the following code and it works fine:

@(Html.Kendo().NumericTextBox<float>().Name("Weight").Min(0).Value(0.05f).Format("#.## kg")
                    .Placeholder("Weight (kg)").HtmlAttributes(new { validate = true })
                )

@(Html.Kendo().NumericTextBox<float>().Name("upTo7").Min(0).Format("#.## €").Value(1.2f)
                    .Placeholder("Price up to 7 days").HtmlAttributes(new { validate = true })
                )

Output:

0.05 kg
1.2 €

I suggest using decimal data type in your database instead of float because approximate numeric data types do not store the exact values specified. See Difference between numeric, float and decimal in SQL Server for more details.

Martin D.
  • 1,950
  • 3
  • 23
  • 33
  • I modified in database from float to decimal (10,2), also in the code. I check with debugger my model, it has proper values like 2.5, 10.5 but NumericTextBox displays values 250,00 and 1050,00 so it's still multiplied by 100. On the other hand look at this screenshot of hidden field https://prnt.sc/p2mr9j It seems to be value from model is correct. I tried @Prasanth-Sankar answer to add decimal attribute but it changes nothing. – Jarzyn Sep 06 '19 at 11:44
  • @Jarzyn Have you tried changing kendo culture: `` – Martin D. Sep 06 '19 at 12:22
  • It was chosen by default, but i added that in jQuery document ready and it changes nothing also. https://prnt.sc/p2nois – Jarzyn Sep 06 '19 at 12:40
  • 1
    i've changed culture to `pl-PL` and it solves the problem, idk why ... i mark your answer, thanks – Jarzyn Oct 21 '19 at 09:49
0

You can try the this..NumericTextBox

@(Html.Kendo().NumericTextBox<decimal>().Name("Weight").Min(0).Value(0.05f).Format("#.## kg")
 .Decimals(3)
                    .Placeholder("Weight (kg)").HtmlAttributes(new { validate = true })

And add Decimal attribute to show how many decimal places need to see in your textbox