/// <summary> /// Converts the number to its equivalant persian string. /// The number should not have more than 3 digits. /// </summary> /// <param name="n">The number to convert</param> /// <returns></returns> private static string ConvertUpTo100(long n) { long t; string result = string.Empty; if (n > 999L) { throw new ArgumentOutOfRangeException("Number is larger than 999"); } if (n > 99L) { t = n / 100; if (PersianLiteral2NumMap.TryNum2PersianString(t * 100, out result)) { n = n - (t * 100); if (n <= 0) { return(result); } else { result += " و "; } } } if (n > 20) { t = n / 10; string tempResult = ""; if (PersianLiteral2NumMap.TryNum2PersianString(t * 10, out tempResult)) { result = result + tempResult; n = n - (t * 10); if (n <= 0) { return(result); } else { result += " و "; } } } if (n > 0) { string tempStr = ""; if (PersianLiteral2NumMap.TryNum2PersianString(n, out tempStr)) { result += tempStr; } } return(result); }
/// <summary> /// Converts a long number to its written form in Persian /// </summary> /// <param name="x">The long integer number to convert</param> /// <returns></returns> /// <exception cref="ArgumentOutOfRangeException"></exception> public static string ToString(long x) { if (x > 999999999999999L) { throw new ArgumentOutOfRangeException("Number is too large to process"); } long t; string result = string.Empty; string unit = ""; if (x < 0L) { result = "منهای "; x = -x; } if (x == 0L) { if (PersianLiteral2NumMap.TryNum2PersianString(x, out result)) { return(result); } } if (x > 999999999999L) { t = x / 1000000000000L; if (PersianLiteral2NumMap.TryNum2PersianString(1000000000000L, out unit)) { result += ConvertUpTo100(t) + " " + unit; x = x - (t * 1000000000000L); if (x <= 0) { return(result); } else { result += " و "; } } } if (x > 999999999L) { t = x / 1000000000L; if (PersianLiteral2NumMap.TryNum2PersianString(1000000000L, out unit)) { result += ConvertUpTo100(t) + " " + unit; x = x - (t * 1000000000L); if (x <= 0) { return(result); } else { result += " و "; } } } if (x > 999999) { t = x / 1000000L; if (PersianLiteral2NumMap.TryNum2PersianString(1000000L, out unit)) { result += ConvertUpTo100(t) + " " + unit; x = x - (t * 1000000L); if (x <= 0) { return(result); } else { result += " و "; } } } if (x > 999) { t = x / 1000; if (PersianLiteral2NumMap.TryNum2PersianString(1000L, out unit)) { if (t != 1) { result += ConvertUpTo100(t) + " "; } result += unit; x = x - (t * 1000L); if (x <= 0) { return(result); } else { result += " و "; } } } if (x > 0) { result += ConvertUpTo100(x); } return(result); }