Add to Technorati Favorites
Welcome to ThePowerShellGuy.com Sign in | Join | Help

Code formatting test

Testing codesamples for viewing and copying (do not link here, I will post this item again later after testing)

If you have problems ppasting in the sample please leave a remark

convert number to Roman notation

Function as PowerShell code (Pre tag CodeHTMLer)

# Convert Int to Roman notation
# /\/\o\/\/ 2006

function format-Roman ($num  ) {  

    $M = [math]::truncate($num / 1000) 
    $num -= $M * 1000 
    $D = [math]::truncate($num / 500) 
    $num -=  $D * 500 
    $C = [math]::truncate($num / 100) 
    $num -=  $C * 100 
    $L = [math]::truncate($num / 50) 
    $num -=  $L * 50 
    $X = [math]::truncate($num / 10) 
    $num -=  $x * 10 
    $V = [math]::truncate($num / 5) 
    $num -=  $V * 5
    
    $Roman = "M" * $M
    $Roman += "D" * $D
    $Roman += "C" * $C
    $Roman += "L" * $L
    $Roman += "X" * $X
    $Roman += "V" * $V
    $Roman += "I" * $num
    
    $roman = $roman.replace('DCCCC','CM') # 900
    $roman = $roman.replace('CCCC','CD') # 400
    $roman = $roman.replace('LXXXX','XC') # 90
    $roman = $roman.replace('XXXX','XL') # 40
    $roman = $roman.replace('VIIII','IX') # 9
    $roman = $roman.replace('IIII','IV') # 4
    
    Return $Roman

}


Commandline example (Block Quote)

 

PoSH>format-Roman 1987
MCMLXXXVII
PoSH>format-Roman 1999
MCMXCIX

PoSH>Update-TypeData C:\PowerShell\TypeData\TypedataInt32.ps1xml
PoSH>(32).toroman()
XXXII

 

format an Integer in Roman notation

 

Type extension as XML (convert whitespace, codehtmler)

 

<?xml version="1.0encoding="utf-8?> 
<Types> 
    <Type> 
        <Name>System.Int32</Name> 
        <Members> 
            <ScriptMethod> 
                <Name>ToRoman</Name> 
                 <Script> 
                    $num = $this

                    $M = [math]::truncate($num / 1000) 
                    $num -= $M * 1000 
                    $D = [math]::truncate($num / 500) 
                    $num -=  $D * 500 
                    $C = [math]::truncate($num / 100) 
                    $num -=  $C * 100 
                    $L = [math]::truncate($num / 50) 
                    $num -=  $L * 50 
                    $X = [math]::truncate($num / 10) 
                    $num -=  $x * 10 
                    $V = [math]::truncate($num / 5) 
                    $num -=  $V * 5
                    
                    $Roman = "M" * $M
                    $Roman += "D" * $D
                    $Roman += "C" * $C
                    $Roman += "L" * $L
                    $Roman += "X" * $X
                    $Roman += "V" * $V
                    $Roman += "I" * $num
                    
                    $roman = $roman.replace('DCCCC','CM') # 900
                    $roman = $roman.replace('CCCC','CD') # 400
                    $roman = $roman.replace('LXXXX','XC') # 90
                    $roman = $roman.replace('XXXX','XL') # 40
                    $roman = $roman.replace('VIIII','IX') # 9
                    $roman = $roman.replace('IIII','IV') # 4
                    
                    Return $roman
                </Script> 
            </ScriptMethod> 

        </Members> 
    </Type> 
</Types>

 

* Edit * Added script again formatted by PowerShell analyzer

 

# Function Format-Roman
# /\/\o\/\/ 006
# http://ThePowerShellGuy.com

# HTML Formatting Generated with PowerShell Analyzer

function format-Roman ($num ) {  

    $M = [math]::truncate($num / 1000) 
    $num -= $M * 1000 
    $D = [math]::truncate($num / 500) 
    $num -=  $D * 500 
    $C = [math]::truncate($num / 100) 
    $num -=  $C * 100 
    $L = [math]::truncate($num / 50) 
    $num -=  $L * 50 
    $X = [math]::truncate($num / 10) 
    $num -=  $x * 10 
    $V = [math]::truncate($num / 5) 
    $num -=  $V * 5
    
    $Roman = "M" * $M
    $Roman += "D" * $D
    $Roman += "C" * $C
    $Roman += "L" * $L
    $Roman += "X" * $X
    $Roman += "V" * $V
    $Roman += "I" * $num
    
    $roman = $roman.replace('DCCCC','CM') # 900
    $roman = $roman.replace('CCCC','CD') # 400
    $roman = $roman.replace('LXXXX','XC') # 90
    $roman = $roman.replace('XXXX','XL') # 40
    $roman = $roman.replace('VIIII','IX') # 9
    $roman = $roman.replace('IIII','IV') # 4
    
    Return $roman

}


 

Enjoy, Greetings /\/\o\/\/

Published Friday, December 22, 2006 2:10 PM by admin
Filed under: ,

Comments

# /\/\o\/\/

Testing Comments

Pasting and wrapping of the Code looks good (PRE tag and Convert whitespace OK when pasted out of IE7 or Bloglines

If you have problems with other RSS readers or browsers, please leave a comment.

Greetings /\/\o\/\/

Friday, December 22, 2006 7:23 PM by admin

# re: Code formatting test

Same post on my Old blog does not linewrap for me in first and last example,

on ThePowerShellGuy.com it looks good

http://mow001.blogspot.com/2006/12/powershell-code-formatting-test-for-my_23.html

/\/\o\/\/

PS Anonymous comment test

Friday, December 22, 2006 9:24 PM by /\/\o\/\/

# re: Code formatting test

Uhm, the code is double spaced...

For me, reading doubly spaced code looks quite hard to read.

But i am not sure how the rest of people would feel about it though.

Sunday, December 24, 2006 11:57 AM by DBMwS

# Welcome to The PowerShell Guy blog

Welcome to my New Blog "The PowerShell Guy" With ( I hope and think ) : A Better Name and address ( http://ThePowerShellGuy.com

Friday, December 29, 2006 8:58 PM by The PowerShell Guy

# re: Code formatting test

Let me know if you are still having issues with CodeHtmler. I'd be happy to try to work them out with you. That said I don't have any problems using CodeHtmler with my CS blog. I actually use Windows Live Writer with my CodeHtmler plug-in (can be found at http://puzzleware.net/codehtmler).

Monday, January 01, 2007 5:19 PM by Wes

# re: Code formatting test

Hiya, Wes

Thanks for the reaction,

I do not have any problems with codeHTMLer,

only with the Formatting on my old blog, (on Blogger) I needed to use the Convert-Whitespace option en piplines and formatting  where gone after an edit, and the HTML changes.

this seems all Blogger related as everything works fine on my new site in CS 2.1

also using PowerShell analyzer now works fine.

(before this became one long line when pasted from the blog)

I posted the same code on my old blog if you want to compare.

Thanks, a very satisfied CodeHTMLer user.

Greetings /\/\o\\/

Tuesday, January 02, 2007 3:29 PM by MoW

# Scripting Games 2007 Advanced PowerShell Event 1 and 2

As the Anwers to the First 2 events in the Scripting Games 2007 , Are Posted I will post my solutions

Wednesday, February 14, 2007 11:31 AM by The PowerShell Guy

# PoSH Challenge part 2 (Level 1-4 )

In former post PoSH Challenge part 1 I started this series, following the the python challenge site using

Friday, January 11, 2008 7:49 PM by The PowerShell Guy
Anonymous comments are disabled