Geeks With Blogs
Points To Share Mostly SharePoint

Here is a piece of code I tested for the conversion of the numbers 0 thru 99 into 20 strings of exactly 2 digits. The first is 04, the next 09, then 14,19,24,29, etc. all the way to 94, 99.

 

The code: "{0:D2}" -f $d formats each number to exactly 2 digits.

The $i used in the “for” loop is interpreted by PowerShell – it automatically assigns it to a float. So when dividing by 5, we get fractions. I needed it to be whole numbers, hence the [math]::floor($i/5).

cls

for ($i = 0; $i -lt 100; $i++)
{
    $n = [math]::floor($i/5)
    $d = $n * 5 + 4
    "{0:D2}" -f $d
}

I erroneously assumed that the result $d will be formattable ("{0:D2}" -f $i renders the desired result), alas this results in the following error:

 

Error formatting a string: Format specifier was invalid..

At E:\HA\testformat.ps1:6 char:2

+ "{0:D2}" -f $d

+ ~~~~~~~~~~~~~~

+ CategoryInfo : InvalidOperation: ({0:D2}:String) [], RuntimeException

+ FullyQualifiedErrorId : FormatError

I tried all sorts of things, but the error was persistent, then I tried coercing – the PowerShell term for casting. I used [float] and [decimal] to no avail, but my 3rd (and obviously last*) attempt was [int] and it worked.

cls

for ($i = 0; $i -lt 100; $i++)

{

$n = [math]::floor($i/5)

[int]$d = $n * 5 + 4

"{0:D2}" -f $d

}

Generated the desired results.

* Why is it that we always find what we look for in the VERY LAST place we looked?

 

That’s all Folks

Posted on Thursday, May 9, 2013 4:17 PM | Back to top


Comments on this post: PowerShell Format Idiosyncrasy

# re: PowerShell Format Idiosyncrasy
Requesting Gravatar...
Hi Dov,

Thanks for attending my "PowerShell for Developers" talk and pointing out your blog post.

This same approach causes an error in C# too.

http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx

The D2 formatter only works on integers.

In C# the below does not even compile.

for (var i = 0; i < 100; i += 1)
{
var n = System.Math.Floor(i / 5);
}

It complains:

The call is ambiguous between the following methods or properties: 'System.Math.Floor(decimal)' and 'System.Math.Floor(double)'

Hope this helps,
Doug

Left by Doug Finke on May 12, 2013 12:18 PM

# re: PowerShell Format Idiosyncrasy
Requesting Gravatar...
I really loved reading your blog. It was very well authored and easy to understand. Unlike additional blogs I have read which are really not that good. I also found your posts very interesting. In fact after reading, I had to go show it to my friend and he enjoyed it as well!
custom editing service || do my assignment for me

Left by Fernando on May 13, 2013 5:48 AM

# re: PowerShell Format Idiosyncrasy
Requesting Gravatar...
Great tutorial Dov, exactly what I was looking for and I'm glad that I've finally found it.
Cheers
Bernardo
Left by Bernardo on Sep 30, 2014 8:05 AM

# cara diet cepat dan mudah
Requesting Gravatar...
I have seen your consciousness about this theme if you post it and it actually provides an informational message to us readers. I'm hoping that you'll proceed writing this type of blog. Thanks for sharing this information.
Left by cara diet cepat dan alami on Nov 28, 2014 12:38 AM

# rumahmurahdijual
Requesting Gravatar...
I'm really enjoying iklanjualrumah reading your well written articles. It appears to be like such as you spend numerous effort and time on your blog. I iklanpropertigratis have bookmarked it and I'm looking forward to reading new articles. Sustain the nice work!
Left by udin on Dec 03, 2014 4:30 AM

# re: PowerShell Format Idiosyncrasy
Requesting Gravatar...
Because homeowners have become more mindful about improving the look of their house, home enhancement industry offers undergone an enormous revolution. Several creating firms are actually bringing clean home renovation tips to the desk. Some companies also have gone on the internet to plunge into this particular new rising market. homebuildingandloan.net
Left by homebuildingandloan.net on Jun 25, 2015 2:54 AM

# re: PowerShell Format Idiosyncrasy
Requesting Gravatar...
Occasionally, your the next door neighbors children have an interest in taking care of your home pets. Yet this is probably not the smartest choice, as kids could be reckless as well as ignore fundamental needs of the animal Because of this, protection from the overall health of your own pets ought to be handed more than in compensated professionals, as you'll get calmness and also the serenity associated with mind when you are not obtainable. dogtrainingphoenix.net
Left by adminpool on Jun 25, 2015 2:55 AM

# re: PowerShell Format Idiosyncrasy
Requesting Gravatar...
great stuff to read thanks.
RS 99 hosting
Left by denny on Sep 02, 2015 8:36 AM

# re: PowerShell Format Idiosyncrasy
Requesting Gravatar...
Several creating firms are actually bringing clean home renovation tips to the desk.
Left by familiar on Apr 09, 2016 7:31 AM

# re: PowerShell Format Idiosyncrasy
Requesting Gravatar...
Thank you so much for this post and I appreciate your effort and work for this and on this too. Hindi Serials must check this thing out
Left by Hindi Serials on Apr 28, 2016 11:27 AM

# re: PowerShell Format Idiosyncrasy
Requesting Gravatar...
I have seen your consciousness about this theme if you post it . But lets see what happens again now on. SSC CGL Tier 1 Admit Card 2016

UP JEE B.Ed Answer Key 2016 – Joint Entrance Exam Merit List @ upbed.nic.in
Left by SSC CGL on Apr 28, 2016 11:29 AM

# PowerShell Format Idiosyncrasy
Requesting Gravatar...
Everytime i see this article.. i i wish to purchase the theme.. Job Alert
Left by Latest Jobs on Jan 20, 2017 6:26 AM

# Result 2017
Requesting Gravatar...
thanks for give best articals
http://www.jobsmafia.in/rms-cet-results-2017-18-www-rashtriyamilitaryschool-in-rms-cet-6th-9th-result-2017-merit-list/
Left by parag on Feb 11, 2017 6:56 AM

# re: Admit Card 2017
Requesting Gravatar...
Thanks For Best News
http://www.jobsmafia.in/mgsu-bikaner-admit-card-2017-mgsubikaner-ac-in-bikaner-university-mgsu-hall-ticket-call-latter-download/
Left by parag on Feb 11, 2017 6:58 AM

# Answer Key 2017
Requesting Gravatar...
very very Plesures you
http://www.jobsmafia.in/hssc-mphw-answer-key-2017-hssc-gov-in-mphw-male-jan-29-official-key-cut-off-marks-download/
Left by parag on Feb 11, 2017 6:59 AM

# re: PowerShell Format Idiosyncrasy
Requesting Gravatar...
thanks for give best articals
Left by Admit Card 2017 on Feb 15, 2017 11:29 AM

# re: PowerShell Format Idiosyncrasy
Requesting Gravatar...
Several creating firms are actually bringing clean home renovation tips to the desk. its awesome article thanks for sharing info with us.
Answer Key 2017
Left by Radhika Patel on Feb 27, 2017 4:05 AM

# re: PowerShell Format Idiosyncrasy
Requesting Gravatar...

I have seen your consciousness about this theme if you post it and it actually provides an informational message to us readers. I'm hoping that you'll proceed writing this type of blog. Thanks for sharing this information.
Left by Admit Card 2017 on May 25, 2017 8:45 AM

# re: PowerShell Format Idiosyncrasy
Requesting Gravatar...
Nice blog and content.
ssc chsl online ssc chsl 2017 result
Left by SSC CHSL on Jun 03, 2017 3:31 AM

# re: PowerShell Format Idiosyncrasy
Requesting Gravatar...
This will be the right weblog for any individual who desires to find out about this topic. You understand so much its virtually hard to argue with you (not that I essentially would want?-HaHa). You absolutely put a new spin on a topic thats been written about for years. Very good stuff, just excellent!
result
Left by rahul roy on Jun 07, 2017 9:04 AM

# re: PowerShell Format Idiosyncrasy
Requesting Gravatar...
Nice Article...Very Useful...Thanks... Tnpsc VAO Group 1 2 Exam Tamilnadu Govt Jobs
Left by Winmeen Tnpsc on Jun 25, 2017 6:32 AM

# re: PowerShell Format Idiosyncrasy
Requesting Gravatar...
check all latest list of various scholarship like upscholarshipstatus
Left by vinay on Jun 25, 2017 10:12 PM

# Admit Card 2017
Requesting Gravatar...
Nice Informstion on post
Result 2017

Answer Key 2017
Left by Result 2017 on Jun 29, 2017 2:47 AM

# NICE
Requesting Gravatar...
Thanks for giving this most wonderful article..i want to share this article to my friends please give your permission.
CG Vyapam PAT Results 2017
Left by uma kumari on Jun 29, 2017 8:26 AM

# Sports news updates today
Requesting Gravatar...
I like this website.. I visit this website daily to read new and awesome articles. Keep Going ...
Sports news updates today
Left by Jaanu on Jul 03, 2017 12:49 AM

# re: PowerShell Format Idiosyncrasy
Requesting Gravatar...
Thanks for the best blog.it was very useful for me. keep sharing such ideas in the future as well.
Admit Card 2017
Left by Result 2017 on Jul 14, 2017 8:25 AM

# re: PowerShell Format Idiosyncrasy
Requesting Gravatar...
Thanks for sharing this awesome article with us.I always like to red these posts.For Govt Jobs Visit
IBPS Recruitment 2017
Left by Vicky on Jul 24, 2017 1:29 AM

# Raksha bandhan sms in hindi for brothers
Requesting Gravatar...
Very informative article thanks for sharing
Raksha bandhan sms in hindi for brothers
Left by manasa on Jul 24, 2017 2:08 AM

# re: PowerShell Format Idiosyncrasy
Requesting Gravatar...
Very useful post. Thanks for sharing. Nice blog.
Sarkari Naukri
Left by Anuj on Aug 11, 2017 3:35 PM

# re: PowerShell Format Idiosyncrasy
Requesting Gravatar...
Hi, absolutely put a new spin on a topic thats been written about for years. Very good stuff, just excellent!onam Pookalam
Left by Sasi on Aug 26, 2017 4:22 AM

# re: PowerShell Format Idiosyncrasy
Requesting Gravatar...
I'm hoping that you'll proceed writing this type of blog. Thanks for sharing this information. onam Pookalam Design
Left by Sasi on Aug 26, 2017 4:23 AM

# re: PowerShell Format Idiosyncrasy
Requesting Gravatar...
Thanks For Sharing Information
Result 2017
Left by Somya on Sep 02, 2017 8:33 AM

# re: PowerShell Format Idiosyncrasy
Requesting Gravatar...
Send cute and bright ecard to add a sparkle to the celebration of your dear ones Happy dussehra images
Left by hulla on Sep 06, 2017 3:30 PM

# re: PowerShell Format Idiosyncrasy
Requesting Gravatar...
Send cute and bright ecard Jobschat.in
Left by namo seo on Sep 15, 2017 3:26 AM

# re: PowerShell Format Idiosyncrasy
Requesting Gravatar...
thanks for this article and you should check latest new of
download aadhar card
download aadhar card
Left by ashu on Oct 25, 2017 10:44 PM

# re: PowerShell Format Idiosyncrasy
Requesting Gravatar...
Its great to hear
KFC " So Good to see you "

Telangana EPass available modern style
Left by TelanganaEpass on Nov 02, 2017 2:13 PM

# re: PowerShell Format Idiosyncrasy
Requesting Gravatar...
It is often seen that a large number of organizations or bloggers start with shared hosting. It makes sense as shared hosting can take good care of websites that do not generate much traffic in their initial period. Also, security is not much of a concern with smaller websites that function according to expectations in face of less network load.
SOURCE: Best Domain Website Hosting Reviews UK 2017
Left by Salman Khan on Nov 18, 2017 1:40 AM

# re: PowerShell Format Idiosyncrasy
Requesting Gravatar...

Tamilnadu state gives the Secondary School Leaving Certificate certification obtained by a student on successful completion of an examination at the end of study at the secondary schooling level in India.

www.dge1.tn.nic.in
www.dge1.tn.nic.in 2018
Left by tn results on Mar 22, 2018 7:07 AM

# re: PowerShell Format Idiosyncrasy
Requesting Gravatar...
Left by Onam 2018 on Apr 11, 2018 6:15 AM

# re: PowerShell Format Idiosyncrasy
Requesting Gravatar...
Everyone knows that Walmart is one of the leading retailer companies in America. Their business operates on a global level. Walmart works with a variety of departmental stores, hypermarkets, and grocery stores.

Walmart Credit Card Login
Left by Walmart Credit Card Login on May 08, 2018 6:24 AM

Your comment:
 (will show your gravatar)


Copyright © PointsToShare | Powered by: GeeksWithBlogs.net