格式化数字 [英] formatting numbers

查看:67
本文介绍了格式化数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

pages = 1


pages.tostring给我1

pages + 1.tostring给我2.0


为什么要添加.0在那里?我在寻找2这个螺丝

我了。


实际上这就是我在做的事情。最后我想要的是打印一个带有前导0的

两位数字。


dim collLetterName as string

collLetterName =" i:\collections \" &安培;格式(现在,yyyy_MMdd)& " \" &安培; _

thisCustno& " _" &安培;格式(现在,yyyyMMdd)& " _" &安培; _

pages + 1.ToString.PadLeft(2," 0")& .pdf


它应该产生i:\collections \2008_0909 \2013420470_20080909_02.p df

相反,我得到 i:\collections\2008_0909 \2013420470_20080909_2.pd f"


格式化这个数字的最佳方法是什么?

解决方案

cj2写道:


pages = 1


pages.tostring给了我1

页面+ 1.tostring给我2.0


为什么它添加.0在那里?我在寻找2这个螺丝

我了。


实际上这就是我在做的事情。最后我想要的是打印一个带有前导0的

两位数字。


dim collLetterName as string

collLetterName =" i:\collections \" &安培;格式(现在,yyyy_MMdd)& " \" &安培; _

thisCustno& " _" &安培;格式(现在,yyyyMMdd)& " _" &安培; _

pages + 1.ToString.PadLeft(2," 0")& .pdf


它应该产生i:\collections \2008_0909 \2013420470_20080909_02.p df

相反,我得到 i:\collections\2008_0909 \2013420470_20080909_2.pd f"


格式化此号码的最佳方式是什么?



也许是因为你用字符串而不是char来调用PadLeft ...


格式化日期时更多不止一次,把它变成一个变量,这样你就可以使用相同的值。您不希望日期在

调用Now之间发生变化,以便您将文件放在错误的文件夹中......


我会喜欢这样做:


collLetterName =

String.Format(" i:\collections \ {0:yyyy_MMdd} \ { 1} _ {0:yyyyMMdd} _ {2:00} .pdf",

现在,thisCustno,pages + 1)


-

G?ran Andersson

_____
http ://www.guffa.com


嗨cj,


根据我的测试,页面+ 1.ToString()"将输出2而不是2.0。

我的测试代码如下:

Dim pages As Integer = 1

Console.WriteLine(pages .ToString())

Console.WriteLine(pages + 1.ToString())

关于你的意外格式输出,我认为是因为

" PadLeft(2," 0")"方法应用于1.ToString()方法。而不是页面+ 1

完全。因此,如果您同时将PadLeft()应用于它们,它将生成

格式,如下所示:


collLetterName =" i:\\ \\collections\" &安培;格式(现在,yyyy_MMdd)& " \" &安培; _

thisCustno& " _" &安培;格式(现在,yyyyMMdd)& " _" &安培; _

(页面+ 1).ToString.PadLeft(2," 0")& < .pdf"


当然,使用String.Format()来控制格式,因为Andersson提供了

而不是使用PadLeft()方法更清晰代码逻辑。


谢谢。


祝你好运,

Jeffrey Tan

Microsoft在线社区支持

====================================== ===

让客户满意是我们的首要任务。我们欢迎您的意见和

有关我们如何改进我们为您提供的支持的建议。请

随时让我的经理知道您对服务水平的看法

提供。您可以直接向我的经理发送反馈:
ms****@microsoft.com


此帖子按原样提供。没有保证,也没有赋予任何权利。


....&格式(页面+ 1,00)& ...


" cj2" < cj*@nospam.nospamwrote in message

news:eK *************** @ TK2MSFTNGP06.phx.gbl ...
< blockquote class =post_quotes>
pages = 1


pages.tostring给我1

pages + 1.tostring给了我2.0


为什么要添加.0在那里?我在寻找2这个螺丝

我了。


实际上这就是我在做的事情。最后我想要的是打印一个带有前导0的

两位数字。


dim collLetterName as string

collLetterName =" i:\collections \" &安培;格式(现在,yyyy_MMdd)& " \" &安培; _

thisCustno& " _" &安培;格式(现在,yyyyMMdd)& " _" &安培; _

pages + 1.ToString.PadLeft(2," 0")& .pdf


它应该产生i:\collections \2008_0909 \2013420470_20080909_02.p df

相反,我得到 i:\collections\2008_0909 \2013420470_20080909_2.pd f"


格式化此号码的最佳方式是什么?


pages = 1

pages.tostring gives me "1"
pages+1.tostring gives me "2.0"

Why did it add the ".0" on there? I was looking for "2" and this screws
me up.

Actually this is what I was doing. In the end what I want is to print a
two digit number with leading 0s.

dim collLetterName as string
collLetterName = "i:\collections\" & Format(Now, "yyyy_MMdd") & "\" & _
thisCustno & "_" & Format(Now, "yyyyMMdd") & "_" & _
pages + 1.ToString.PadLeft(2, "0") & ".pdf"

It should produce "i:\collections\2008_0909\2013420470_20080909_02.p df"
Instead I get "i:\collections\2008_0909\2013420470_20080909_2.pd f"

How is the best way to format my number for this use?

解决方案

cj2 wrote:

pages = 1

pages.tostring gives me "1"
pages+1.tostring gives me "2.0"

Why did it add the ".0" on there? I was looking for "2" and this screws
me up.

Actually this is what I was doing. In the end what I want is to print a
two digit number with leading 0s.

dim collLetterName as string
collLetterName = "i:\collections\" & Format(Now, "yyyy_MMdd") & "\" & _
thisCustno & "_" & Format(Now, "yyyyMMdd") & "_" & _
pages + 1.ToString.PadLeft(2, "0") & ".pdf"

It should produce "i:\collections\2008_0909\2013420470_20080909_02.p df"
Instead I get "i:\collections\2008_0909\2013420470_20080909_2.pd f"

How is the best way to format my number for this use?

Perhaps because you are calling PadLeft with a string instead of a char...

When you format the date more than once, get it into a variable so that
you use the same value. You don''t want the date to change between the
calls to Now so that you get the file in the wrong folder...

I would prefer to do it like this:

collLetterName =
String.Format("i:\collections\{0:yyyy_MMdd}\{1}_{0 :yyyyMMdd}_{2:00}.pdf",
Now, thisCustno, pages + 1)

--
G?ran Andersson
_____
http://www.guffa.com


Hi cj,

Based on my test, "pages + 1.ToString()" will output "2" instead of "2.0".
My test code is listed below:
Dim pages As Integer = 1
Console.WriteLine(pages.ToString())
Console.WriteLine(pages + 1.ToString())

Regarding your unexpected formatting output, I think that is because
"PadLeft(2, "0")" method is applied on "1.ToString()" instead of "pages+1"
entirely. So if you apply PadLeft() to them together, it will generate the
formatting as you want, like this:

collLetterName = "i:\collections\" & Format(Now, "yyyy_MMdd") & "\" & _
thisCustno & "_" & Format(Now, "yyyyMMdd") & "_" & _
(pages + 1).ToString.PadLeft(2, "0") & ".pdf"

Sure, using String.Format() to control the formatting as Andersson provided
instead of using PadLeft() method gives a clearer code logic.

Thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
=========================================
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

This posting is provided "AS IS" with no warranties, and confers no rights.


.... & Format(pages + 1,"00") & ...

"cj2" <cj*@nospam.nospamwrote in message
news:eK***************@TK2MSFTNGP06.phx.gbl...

pages = 1

pages.tostring gives me "1"
pages+1.tostring gives me "2.0"

Why did it add the ".0" on there? I was looking for "2" and this screws
me up.

Actually this is what I was doing. In the end what I want is to print a
two digit number with leading 0s.

dim collLetterName as string
collLetterName = "i:\collections\" & Format(Now, "yyyy_MMdd") & "\" & _
thisCustno & "_" & Format(Now, "yyyyMMdd") & "_" & _
pages + 1.ToString.PadLeft(2, "0") & ".pdf"

It should produce "i:\collections\2008_0909\2013420470_20080909_02.p df"
Instead I get "i:\collections\2008_0909\2013420470_20080909_2.pd f"

How is the best way to format my number for this use?


这篇关于格式化数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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