Archive

Archive for June, 2012

Measure Drawing Size Of String

Shared Function MeasureDrawingSizeOfString(ByVal text As String) As SizeF
Dim bmp As New System.Drawing.Bitmap(400, 400)
Dim graphic As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(bmp)
Dim font As New System.Drawing.Font(“Arial”, 10)
‘Dim width As Integer = graphic.MeasureString(text, font).Width
‘Dim height As Integer = graphic.MeasureString(text, font).Height
Return graphic.MeasureString(text, font)
End Function

Advertisements
Categories: C#

Indian Currency Numbering

using System;
using System.Text;

namespace WindowsFormsTestC
{
class IndianNumbering
{
/// <summary>
/// retrun number to String
/// Max limit is the limit of interger
/// </summary>
/// <param name=”numberToConvert”></param>
/// <param name=”allowAndInLast”></param>
/// <returns></returns>
public static string NumberToText(int numberToConvert, bool allowAndInLast=false)
{
if (numberToConvert == 0) return “Zero”;

string and = allowAndInLast ? “and ” : “”;
if (numberToConvert == -2147483648) return “Minus Two Hundred ” + and + “Fourteen Crore Seventy Four Lakh Eighty Three Thousand Six Hundred ” + and + “Forty Eight”;

int[] numbers = new int[4];
int first = 0;
int LooseChange, multipleof100, multipleof10;
System.Text.StringBuilder wordholder = new System.Text.StringBuilder();

if (numberToConvert < 0)
{
wordholder.Append(“Minus “);
numberToConvert = -numberToConvert;
}
string[] wordsFor1To10 = { “”, “One “, “Two “, “Three “, “Four “, “Five “, “Six “, “Seven “, “Eight “, “Nine ” };
string[] wordsFor11To20 = { “Ten “, “Eleven “, “Twelve “, “Thirteen “, “Fourteen “, “Fifteen “, “Sixteen “, “Seventeen “, “Eighteen “, “Nineteen ” };
string[] wordsFactor10 = { “Twenty “, “Thirty “, “Forty “, “Fifty “, “Sixty “, “Seventy “, “Eighty “, “Ninety ” };
string[] wordsBigLimit = { “Thousand “, “Lakh “, “Crore ” };
numbers[0] = numberToConvert % 1000; // base units
numbers[1] = numberToConvert / 1000;
numbers[2] = numberToConvert / 100000;
numbers[1] = numbers[1] – 100 * numbers[2]; // thousands

numbers[2] = numbers[2] – 100 * numbers[3]; // lakhs

numbers[3] = numberToConvert / 10000000; // crores
for (int i = 3; i > 0; i–)
{
if (numbers[i] != 0)
{
first = i;
break;
}
}

for (int i = first; i >= 0; i–)
{
if (numbers[i] == 0) continue;

LooseChange = numbers[i] % 10; // base 1 to 10
multipleof10 = numbers[i] / 10; // factor of 10
multipleof100 = numbers[i] / 100; // factor of hundreds
multipleof10 = multipleof10 – 10 * multipleof100; // multiply of tens

if (multipleof100 > 0) wordholder.Append(wordsFor1To10[multipleof100] + “Hundred “);
if (LooseChange > 0 || multipleof10 > 0)
{
if (multipleof100 > 0 || i < first) wordholder.Append(and);

if (multipleof10 == 0)
wordholder.Append(wordsFor1To10[LooseChange]);
else if (multipleof10 == 1)
wordholder.Append(wordsFor11To20[LooseChange]);
else
wordholder.Append(wordsFactor10[multipleof10 – 2] + wordsFor1To10[LooseChange]);
}
if (i != 0) wordholder.Append(wordsBigLimit[i – 1]);
}
return wordholder.ToString().TrimEnd();
}
}
}

Categories: .Net, C#