jQuery UI Slider and jQuery Globalization with value delimited by comma [ , ]

浪子不回头ぞ 提交于 2020-01-16 16:15:11

问题


I'm developing an ASP.NET MVC 3 app that uses both jQuery Globalization and jQuery UI Slider.

I'm localizing this app to Portuguese from Brazil (pt-BR) and it also supports English.

The problem is that when I change the language to Portuguese, all Model float values come delimited by a comma ( , ) like 7,7. I then try to set the Slider initial value but it only accepts the value with a dot ( . ) like 7.7.

To overcome such situation I'm doing this:

$.CreateSlider("#sliderHeight", "#Height", @Model.Height.FormatValue(), 0, 2.5, 0.01);

FormatValue is an extension method:

public static string FormatValue(this float value)
{
    return value.Value.ToString().Replace(",", ".");
}

You see that I have to replace the comma delimiter with a dot delimiter so that I can set the jQuery Slider value.

I then have to use jQuery Globalization to show the correct value in the UI this way:

$(textBox).val($.global.format(ui.value, 'n2'));

It works at least...

My question is:

Is there a better or standard way of doing this?

The thing is that the Model values are coming the way they should, that is, as I switched the culture to pt-BR the numbers come ( , ) delimited. This is correct in my point of view. The limitation is in the way the jQuery Slider receives the value.

Just to complement: if I tried to pass the values the way they come from the @Model, I'd have something like this, that obviously would break (because the parameters values are themselves separated by commas):

$.CreateSlider("#sliderHeight", "#Height", 7,7 , 0, 2.5, 0.01);

回答1:


Your slider accepts InvariantCulture values, so you need to provide such. Just modify your method:

public static string ParseValue(this float value)
{
    return value.Value.ToString(CultureInfo.InvariantCulture);
}

BTW. ParseValue is not the best name here as no parsing going on. FormatValue would sound better.
BTW. Your original method won't work correctly for some cultures (might not be visible for small values but for greater than i.e. 100 bad things could happen). If you need a dot as floating point, always use InvariantCulture.



来源:https://stackoverflow.com/questions/5861209/jquery-ui-slider-and-jquery-globalization-with-value-delimited-by-comma

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!