C#填充量为零 [英] C# Padding Amount With Zeros

查看:108
本文介绍了C#填充量为零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 amount 字段,该字段在数据库中为小数。
我需要始终用小数点左边的10个数字和后面两个数字显示此金额。

I have an amount field which is a decimal in the database. I need to always display this amount with 10 numbers on the left of the decimal and two after.

示例:

金额= 245.00 ,应显示为 0000000245.00
此外,金额可能超过1,000或10,000,应显示为:

Example:
Amount = 245.00 which should display as 0000000245.00 Additionally, the amount could be over 1,000 or 10,000 which should display as:

0000001245.00 0000011245.00

如何格式化金额,使其始终具有小数位数的小数位数左侧的适当数量的零?

How can I format the amount to always have the appropriate number of zeros on the left side of the decimal with a variable amount size?

推荐答案

您应在格式字符串中输入0。

You should put 0's in your format string. Something like this.

myValue.ToString("0000000000.00");

这将始终为您提供小数点左侧的10位数字和右侧的两位数字。

That will always give you 10 digits on the left side of your decimal point and two on the right.

如果您不希望右侧有两位数字,请执行此操作。

If you do not want two digits on the right side... do this.

myValue.ToString("0000000000.##");

这说嘿,如果我们有一个值显示它;否则,请跳过它。

This says hey if we have a value display it; otherwise, skip it.

这篇关于C#填充量为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆