我正在使用ASP MVC 5和DataTables插件,但是在格式化小数点时遇到一些问题。
我需要将数字格式设置为以点作为千位分隔符,以逗号作为十进制分隔符。
我正在尝试使用DisplayFormat
来实现,但是由于我以Json的身份返回数据,因此它只是不适用
这是我的问题的一个小例子
public class MyClass
{
[DisplayFormat(DataFormatString = "{0:0.##}")]
decimal MyDecimal {get;set;}
}
在控制器中
public JsonResult LoadDataTables()
{
using(var db = new ExampleContext())
{
var data = db.MyRepository.Select(x => new MyClass
{
MyDecimal = x.mydecimal
}).ToList();
//i'm ignoring a lot of processing to create a short snippet
return Json(new { draw = draw, recordsFiltered = recordsTotal, recordsTotal = recordsTotal, data = data });
}
}
然后,在前端,json带有逗号(千位)和点(十进制分隔符)
我该如何处理?采用某种方法CultureInfo
还是手动设置转换?
编辑:在我看来,这就是我加载数据的方式
var table = $('#myTabla').DataTable({
processing: true,
serverSide: true,
filter: true,
orderMulti: false,
paging: true,
pageLength: 10,
ajax: {
"url": '@Url.Action("LoadDataTables")',
"type": "POST",
"datatype": "json",
});
谢谢!
答案 0 :(得分:1)
您需要设置自定义文化:
System.Globalization.CultureInfo customCulture = (System.Globalization.CultureInfo)
System.Threading.Thread.CurrentThread.CurrentCulture.Clone();
customCulture.NumberFormat.NumberDecimalSeparator = ",";
System.Threading.Thread.CurrentThread.CurrentCulture = customCulture;
谢谢。
答案 1 :(得分:1)
1-您可以按以下另一种方式进行操作:
public class MyClass
{
MyNumber MyDecimal {get;set;}
}
struct MyNumber
{
decimal Value {get;set;}
ToString()
{
// Adapt number Format and CultureInfo as wanted
return value.ToString("0:0.##", MyCultureInfo);
}
}
用途:
十进制d = MyNumber.Value
string s =(string)MyNumber;或MyNumber.ToString();
2-或在客户端:
// Invert . and ,
// 1.22222.5689,56
// to 1,22222,5689.56
String.prototype.formatNumber = function(number)
{
return number.replace(',', ';').replace('.', ',').replace(';', '.');
}
3-在C#中操作JSon的最佳选择是:https://www.newtonsoft.com/json
您可以序列化和反序列化json并创建自定义转换。
答案 2 :(得分:1)
您可以为此使用数字助手。您可以访问此链接以引用更多信息:https://datatables.net/manual/data/renderers#Number-helper
这里是一个样本。希望对您有帮助,我的朋友:))
//控制器中的代码
public class Test
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Currency { get; set; }
}
private List<Test> Data()
{
var data = new List<Test>
{
new Test{ Id = 1, Name = "A1", Currency = 1000000.00M},
new Test{ Id = 2, Name = "A2", Currency= 50000000.12M},
new Test{ Id = 3, Name = "A3", Currency = 3000000.45M},
new Test{ Id = 4, Name = "A4", Currency = 20000}
};
return data;
}
[HttpPost]
public JsonResult LoadDataTables()
{
var data = Data();
var recordsTotal = Data().Count;
var recordsFiltered = Data().Count();
string draw = Request.Form.GetValues("draw")[0];
return Json(new { draw = Convert.ToInt32(draw), recordsTotal = recordsTotal, recordsFiltered = recordsFiltered, data = data }, JsonRequestBehavior.AllowGet);
}
//查看
<table id="myTable" >
<thead>
<tr>
<td >Id</td>
<td >Name</td>
<td>Currency</td>
</tr>
</thead>
<tbody>
</tbody>
</table>
var table = $('#myTable').DataTable({
processing: true,
serverSide: true,
filter: true,
orderMulti: false,
paging: true,
pageLength: 10,
ajax: {
"url": '@Url.Action("LoadDataTables", "Home")',
"type": "POST",
"datatype": "json",
},
"columns": [
{
"data": "Id"
}, {
"data": "Name"
}, {
"data": "Currency",
render: $.fn.dataTable.render.number('.', ',', 2, '')
}
]
});