Created
November 5, 2015 15:17
-
-
Save kwestground/b30655d5b5be7f72bd3d to your computer and use it in GitHub Desktop.
SboItemExtensions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using SAPbouiCOM; | |
using System.Globalization; | |
using System.Text.RegularExpressions; | |
namespace SboHelpers | |
{ | |
public static class SboItemExtensions | |
{ | |
public static MoneyField GetMoney(this EditText editText) | |
{ | |
var moneyString = editText.Value.Substring(0, editText.Value.Length - 4); | |
var money = decimal.Parse(moneyString, NumberStyles.Any); | |
var currencyCode = editText.Value.Substring(editText.Value.Length - 3); | |
return new MoneyField | |
{ | |
Money = money, | |
CurrencyCode = currencyCode | |
}; | |
} | |
/// <summary> | |
/// Parse weight field eg. 100kg into mg | |
/// </summary> | |
/// <param name="editText"></param> | |
/// <returns>milligrams</returns> | |
public static int GetWeightInMg(this EditText editText) | |
{ | |
var weightInUnit = decimal.Parse(Regex.Match(editText.Value, @"[\d.]+").Value, NumberStyles.Any); | |
var unit = Regex.Match(editText.Value, @"[A-Za-z]+").Value; | |
switch (unit) | |
{ | |
case "g": | |
return (int)weightInUnit * 1000; | |
case "kg": | |
return (int)weightInUnit * 1000000; | |
} | |
return 0; | |
} | |
} | |
public class MoneyField | |
{ | |
public decimal Money { get; set; } | |
public string CurrencyCode { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment