最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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

发布评论

评论列表(0)

  1. 暂无评论