private void AppendDecimalUnits(StringBuilder result, ThousandBlock modBlock) { result.Append(" "); if (modBlock.IsUnitary()) { result.Append(formato.GetUnidadeDecimalNoSingular()); } else { result.Append(formato.GetUnidadeDecimalNoPlural()); } }
public string ToWords(double number) { try { if (number < 0) { throw new ArgumentException("Não é possível transformar números negativos."); } StringBuilder result = new StringBuilder(); if (number == 0) { result.Append(GetNumber(0)); } else { string[] parts = Split(number); string formattedInt = parts[0]; string[] ints = formattedInt.Split(','); ThousandBlock[] blocks = new ThousandBlock[ints.Length]; for (int i = 0; i < blocks.Length; i++) { string block = ints[i]; blocks[i] = new ThousandBlock(this, block); } string formattedMod = parts[1]; ThousandBlock modBlock = new ThousandBlock(this, formattedMod); bool hasMod = !modBlock.IsZero(); bool hasInteger = (blocks.Length > 1) || (!blocks[blocks.Length - 1].IsZero()); if (hasInteger) { AppendIntegers(result, blocks); AppendIntegersUnits(number, result, blocks); } if (hasInteger && hasMod) { result.Append(GetAndSeparator()); } if (hasMod) { AppendIntegers(result, modBlock); AppendDecimalUnits(result, modBlock); } } return(result.ToString()); } catch (MissingManifestResourceException) { throw new ArgumentException("Número muito grande para ser transformado em extenso."); } }
public string ToWords(long number) { try { if (number < 0) { throw new ArgumentException("Não é possível transformar números negativos."); } StringBuilder result = new StringBuilder(); if (number == 0) { result.Append(GetNumber(0)); } else { NumberFormatInfo numberFormatInfo = new NumberFormatInfo(); numberFormatInfo.NumberGroupSeparator = ","; numberFormatInfo.NumberDecimalSeparator = "."; decimal decimalNumber = Convert.ToDecimal(number); string formattedInt = decimalNumber.ToString("###,###", numberFormatInfo); string[] ints = formattedInt.Split(','); ThousandBlock[] blocks = new ThousandBlock[ints.Length]; for (int i = 0; i < blocks.Length; i++) { string block = ints[i]; blocks[i] = new ThousandBlock(this, block); } AppendIntegers(result, blocks); AppendIntegersUnits(number, result, blocks); } return(result.ToString()); } catch (MissingManifestResourceException) { throw new ArgumentException("Número muito grande para ser transformado em extenso."); } }
private void AppendIntegers(StringBuilder result, params ThousandBlock[] blocks) { bool hasStarted = false; for (int i = 0; i < blocks.Length; i++) { ThousandBlock thousandBlock = blocks[i]; if (!(hasStarted && thousandBlock.IsZero())) { int thousandPower = (blocks.Length - i - 1); if (hasStarted) { if (thousandBlock.IsUnitary()) { result.Append(GetAndSeparator()); } else { if (thousandPower < 1) { result.Append(GetAndSeparator()); } else { result.Append(GetThousandSeparator()); } } } result.Append(thousandBlock.ToWords()); if (thousandPower > 0) { result.Append(" "); result.Append(GetString("1e" + 3 * thousandPower + (thousandBlock.IsUnitary() ? "singular" : "plural"))); } hasStarted = true; } } }