我得到了那个小代码示例来解释我需要做什么
SS[, .(NumTrans = sum(NumTrans)), by = Cust_map]
输出
2016-03-03T12:11:10.123预期2016-03-03T12:11:10.123
b 2016-03-03T12:11:10.12预期2016-03-03T12:11:10.12
c 2016-03-03T12:11:10.1预期2016-03-03T12:11:10.1
d 2016-03-03T12:11:1预计2016-03-03T12:11:10
你可以看到我的方法在最后一种情况下删除了太多。
如何在不触及秒部分的情况下删除尾随using System;
namespace ConsoleApplication1
{
public class Program
{
public static void Main(string[] args)
{
const string format = "yyyy-MM-ddTHH:mm:ss.fff";
var trimTime = new char[] { '0', '.' };
var a = new DateTime(2016, 3, 3, 12, 11, 10, 123);
var b = new DateTime(2016, 3, 3, 12, 11, 10, 120);
var c = new DateTime(2016, 3, 3, 12, 11, 10, 100);
var d = new DateTime(2016, 3, 3, 12, 11, 10, 000);
Console.WriteLine("a {0} expected 2016-03-03T12:11:10.123", a.ToString(format).TrimEnd(trimTime));
Console.WriteLine("b {0} expected 2016-03-03T12:11:10.12", b.ToString(format).TrimEnd(trimTime));
Console.WriteLine("c {0} expected 2016-03-03T12:11:10.1", c.ToString(format).TrimEnd(trimTime));
Console.WriteLine("d {0} expected 2016-03-03T12:11:10", d.ToString(format).TrimEnd(trimTime));
}
}
}
和0
的解决方案是什么?
答案 0 :(得分:3)
只需将格式字符串更改为大写F
s:
string format = "yyyy-MM-ddTHH:mm:ss.FFF";
来自MSDN:
“FFF”自定义格式说明符
“FFF”自定义格式说明符表示秒部分的三个最高有效数字;也就是说,它表示日期和时间值中的毫秒数。但是,尾随零或三位零数字不会显示。
输出(从每行中删除TrimEnd
:
a 2016-03-03T12:11:10.123 expected 2016-03-03T12:11:10.123
b 2016-03-03T12:11:10.12 expected 2016-03-03T12:11:10.12
c 2016-03-03T12:11:10.1 expected 2016-03-03T12:11:10.1
d 2016-03-03T12:11:10 expected 2016-03-03T12:11:10