C#: Extract number out of string, then change comma(,) to dot(.) - c#

I'm using Visual Web Ripper to extract name and prices on products on a website.
When i extract the price from a table it comes in a form like this:
Kr. 129,30
I need to extract the 129,30, then turn the comma to a dot (129.30).
Visual Web Ripper can use scripts to modify the extracted content. It can use standard Regex, C# and VB.NET.
In the Regex tab I have found that
(\d+.)?(\d+)(.\d+)?
gives me 129,30, but then I can't change the comma into a dot.
Therefor I have to use C#. It comes with this standard script:
using System;
using VisualWebRipper.Internal.SimpleHtmlParser;
using VisualWebRipper;
public class Script
{
//See help for a definition of WrContentTransformationArguments.
public static string TransformContent(WrContentTransformationArguments args)
{
try
{
//Place your transformation code here.
//This example just returns the input data
return args.Content;
}
catch(Exception exp)
{
//Place error handling here
args.WriteDebug("Custom script error: " + exp.Message);
return "Custom script error";
}
}
}
How do I modify it to extract the number then replace the comma with a dot?

This is obviously Krona, so we should use the Swedish culture info to translate it. First we start with the input:
var original = "Kr. 129,30";
Get the culture:
using System.Globalization;
var culture = CultureInfo.GetCultureInfo("sv-SE");
This culture expects the currency string to be kr (case insensitive) but we have Kr.. So let's update it:
var format = (NumberFormatInfo)culture.NumberFormat.Clone();
format.CurrencySymbol = "Kr.";
And now the culture aware parse:
var number = Decimal.Parse(original, NumberStyles.Currency, format);
Now number contains a decimal that has been parsed correctly.

String.Replace is an option ( text.Replace(",", ".")).
It would be better to properly parse number with correct CultureInfo and than reformat it back with InvariantCulture.

Related

Format Currency string in c# without currency code

I am trying to format a double to currency string in c#
normally, I would use the following code:
using System;
using System.Globalization;
class Demo {
static void Main() {
double value = 234.66;
Console.WriteLine(value.ToString("C", CultureInfo.InvariantCulture));
Console.WriteLine(value.ToString("C3", CultureInfo.CurrentCulture));
}
}
issue:
The first format prepends an unwanted special caracter: ยค234.66
the later one pepends a dollar sign: $234.660
for normal usecases, I could use several culture infos such as in C# formatting currency given currency code (like USD / GBP / FRF)
unfortunately, Crypto currencies are not supported as far as I know of. So I either look for no currency symbol at all (adding it later to the string) or for a custom currency symbol.
What was quite close was to use balance.ToString("0.##") but in case of 104.10 it would make 104.1 out of it..
var clone = (CultureInfo)CultureInfo.InvariantCulture.Clone();
clone.NumberFormat.CurrencySymbol = "";
var currency = 104.67m;
var stringCurrency = currency.ToString("C", clone);

How can I format string values using a mask in a generic way?

Background: We have a system that receives data from another backend system. We handle the displaying of that data, and we have our own XML templates to control how certain things are displayed (i.e. we have our own column templates to dictate what the column headers are, etc.) One thing we would like to support is the ability to provide a mask for these column templates that would apply to the values coming from the backend. Below is a scenario that I'm having trouble with.
Problem: I can't seem to get a simple string format working. I'd like to format a STRING value of four digits (i.e. "1444") in a time format (i.e. "14:44"). I've tried:
String.Format("{0:00:00}", "1444")
Note the importance of the input being a STRING. If I supply an int value, the format will work. The reason I cannot use this is because all the data we receive from the backend is in string format, and we'd like for this to be generic (so casting isn't really an option).
By generic, I mean I'd like to specify a mask in our own XML templates, something like:
<MyColumnTemplate Id="MyColumn" Mask="00:00" />
and to use that mask in a string format call for string values? If the mask fails, we could just simply return the original value (as the String.Format() method already does by default).
Edit: To help clarify, here is a simplified version of what I'd like to be able to do in code:
string inputValue = "1444";
string maskValueFromXml = "00:00";
string mask = "{0:" + maskValueFromXml + "}";
string myDesiredEndResult = String.Format(mask, inputValue);
The thing is you are working string to string,since you ask for time and phone number they are all numbers then try this trick(if we can call it that :)):
string result = string.Format("{0:00:00}", int.Parse("1444"));
For phone number:
string result = string.Format("{0:000-000-0000}", int.Parse("1234560789"));
You can even place your desired masks in a dictionary for example and do this:
Dictionary<string, string> masks = new Dictionary<string, string>();
masks.Add("Phone", "{0:000-000-0000}");
masks.Add("Time", "{0:00:00}");
string test = "1234560789";
string result = string.Format(masks["Phone"], int.Parse(test));
Try with DateTime.TryParseExact, e.g:
DateTime dateEntered;
string input = "1444";
if (DateTime.TryParseExact(input, "HH:mm", System.Globalization.CultureInfo.CurrentCulture, System.Globalization.DateTimeStyles.None, out dateEntered))
{
MessageBox.Show(dateEntered.ToString());
}
else
{
MessageBox.Show("You need to enter valid 24hr time");
}
After that, you can use string.Format, predefined formats on MSDN.

Adding comma between longer numbers [duplicate]

This question already has answers here:
.NET String.Format() to add commas in thousands place for a number
(23 answers)
Closed 9 years ago.
I'm trying to put comma's between long numbers automatically, but so far without success. I'm probably making a very simple mistake, but so far I can't figure it out. This is the code I currently have, but for some reason I'm getting 123456789 as the output.
string s = "123456789";
string.Format("{0:#,###0}", s);
MessageBox.Show(s); // Needs to output 123,456,789
var input = 123456789;
// these two lines amount to the same thing
Console.WriteLine(input.ToString("N0"));
Console.WriteLine(string.Format("{0:N0}", input));
If, as per your question, you need to start with a string:
var stringInput = "123456789";
var input = int.Parse(stringInput);
// these two lines amount to the same thing
Console.WriteLine(input.ToString("N0"));
Console.WriteLine(string.Format("{0:N0}", input));
You'll possibly also need to take culture into account when parsing/formatting. See the overloads that take an IFormatProvider.
Try this:
string value = string.Format("{0:#,###0}", 123456789);
In your code you are missing the initial { in the format string, and then number formatting options apply to numbers, while your s is a string.
You could convert the string to a number with int.Parse:
int s = int.Parse("123456789");
string value = string.Format("{0:#,###0}", 123456789);
MessageBox.Show(value);
This should work (you need to pass String.Format() a number, not another String):
Int32 i = 123456789;
String s = String.Format("{0:#,###0}", i);
MessageBox.Show(s);
But consider the format string you're using...there are cleaner options available, as others are suggesting.
Look at the number formatting information on MSDN: Standard Numeric Format Strings, or optionally at the custom format strings: Custom Numeric Format Strings.
For custom number formats:
The "," character serves as both a group separator and a number scaling specifier.
double value = 1234567890;
Console.WriteLine(value.ToString("#,#", CultureInfo.InvariantCulture));
// Displays 1,234,567,890
Console.WriteLine(value.ToString("#,##0,,", CultureInfo.InvariantCulture));
// Displays 1,235
There is so much wrong with your code, that's it's hard to describe every detail.
Look at this example:
namespace ConsoleApplication1
{
using System;
public class Program
{
public static void Main()
{
const int Number = 123456789;
var formatted = string.Format("{0:#,###0}", Number);
Console.WriteLine(formatted);
Console.ReadLine();
}
}
}

C# - can I parse back text that have already formatted to currency?

this is function to format text that contains numbers only into $ currency
private String GLOBALIZE_TEXT(String originalText)
{
decimal parsed;
CultureInfo myCultureInfo;
string formattedText = "";
//use try catch to prevent larger inputs
try
{
parsed = decimal.Parse(originalText, CultureInfo.InvariantCulture);
myCultureInfo = new CultureInfo("$");
formattedText = string.Format(myCultureInfo, "{0:c}", parsed);
}
catch (Exception ignorethis)
{
}
return formattedText;
}
now in usage:
String myString = "3821";
myString = GLOBALIZE_TEXT(myString);
//now my String becomes "$3,821.00"
question is, can I parse back that "$3,821.00" to "3821" again?
I need to parse it back so I can use it as an integer where "3821" can be converted by Convert.ToInt32("3821").
or maybe that parsed String can also be converted directly to string?
Please let me know your opinion.
You can try:
double.Parse(myString, NumberStyles.Currency);
More information on the NumberStyles enum can be found on MSDN here and more information on this specific double.Parse method can be found on MSDN here.
Maybe it's best to ask why you need to do this? You should always try and store a value in it's native format. If you need to do it from a captured or imported string then I would go the route of using a regular expression to remove it.
Regular expression to remove any currency symbol from a string?

parsing a string into int/long using custom format strings

In C#.Net, here's a simple example of how to format numbers into strings using custom format strings:
(example taken from: http://www.csharp-examples.net/string-format-int/)
String.Format("{0:+### ### ### ###}", 447900123456); // "+447 900 123 456"
String.Format("{0:##-####-####}", 8958712551); // "89-5871-2551"
Is there a way to convert this formatted string back into a long/integer ? Is there someway to do this :
long PhoneNumber = Int32.Parse("89-5871-2551", "{0:##-####-####}");
I saw that DateTime has a method ParseExact which can do this work well. But I did not see any such thing for int/long/decimal/double.
You can regex out all of the non numeric numbers, and what you're left with is a string of numbers that you can parse.
var myPhoneNumber = "89-5871-2551";
var strippedPhoneNumber = Regex.Replace(myPhoneNumber, #"[^\d]", "");
int intRepresentation;
if (Int32.TryParse(strippedPhoneNumber, out intRepresentation))
{
// It was assigned, intRepresentation = 8958712551
// now you can use intRepresentation.
} else {
// It was not assigned, intRepresentation is still null.
}
Well, you can always do
long PhoneNumber = Int32.Parse("89-5871-2551".
Replace(new char[]{'-','+',whatever..}).Trim());
By the way, considering that you're parsing a string received from some IO, I would suggest to use more secure (in terms of conversion) Int32.TryParse method.
The way like you described doesn't actually exist.
Just Regex out all of the non-numeric characters, then parse that string.

Categories