javascript - What is the easy way to show only 4 digits after decimal point by default inside textboxes in html - Stack Overflow

I have some textboxes which are bind to decimal fields of a view model in cshtml file. Most of them hav

I have some textboxes which are bind to decimal fields of a view model in cshtml file. Most of them have nine zeros after decimal. I want to show only 4 digits by default while editing them. However, I don't want to fix the length after decimal. Can anyone help me with this?

I have some textboxes which are bind to decimal fields of a view model in cshtml file. Most of them have nine zeros after decimal. I want to show only 4 digits by default while editing them. However, I don't want to fix the length after decimal. Can anyone help me with this?

Share edited Sep 5, 2018 at 4:10 Exception handler asked Sep 5, 2018 at 4:02 Exception handlerException handler 1721 gold badge4 silver badges17 bronze badges 1
  • Why wouldn't your view model already have the value calculated to the correct number of digits before it reaches the view? – Erik Philips Commented Sep 5, 2018 at 4:47
Add a ment  | 

3 Answers 3

Reset to default 3

I suggest doing at c# controller or where you bind data to UI control like:

String.Format("{0:0.0000}", 123.456789012); // 123.4568

You can aslo do it in native javascript like :

var num = 123.456789012;
var n = num.toFixed(4); // 123.4568
var n = 56767.87488958865
console.log(n.toFixed(4)) //56767.8748

Or you can use CultureInfo in C#.

CultureInfo nfi = new CultureInfo("en-US", False);
nfi.NumberFormat.CurrencyDecimalDigits = 2;
nfi.NumberFormat.CurrencyDecimalSeparator = ".";
nfi.NumberFormat.CurrencyGroupSeparator = "";
nfi.NumberFormat.NumberDecimalDigits = 2;
nfi.NumberFormat.NumberDecimalSeparator = ".";
nfi.NumberFormat.NumberGroupSeparator = "";

//Now you can show the data like
txtValue.Text = data.ToString("N", nfi); //Format as number
txtValue.Text = data.ToString("C", nfi); //Format as currency

Maybe this will help you in the right direction

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744857552a4597497.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信