convert string with dots to currency with euro, - c#

I have a string like this : "44.00000000000"
The dot is decimal point in this case, I am trying to convert to Euro currency. but having problems
Please see the screenshot

Change your decimal parse line to use the following:
decimal.Parse(inputString, CultureInfo.InvariantCulture)
You may want to have a read around here:
http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.invariantculture.aspx

Related

Regex to convert string number to currency [duplicate]

This question already has answers here:
Currency format for display
(8 answers)
Closed 8 years ago.
I have a string filed which holding the dollar amount. What I want to do is process the string and covert in actual currency.
Example:
Possible incoming string;
80
1000.00
1000.0
-100
Desired Outputs are;
100.00
1,000.00
-100.00
How can I format the string with regex and covert it to the output I want?
It's not really clear what you mean by actual currency, but I'm assuming you just want a string that looks like the samples you've posted. Keep in mind that the decimal type (the type best suited to represent currency) doesn't have any actual formatting information.
You'll have to parse the string using decimal.Parse, then convert the value back to a string to get it into the desired format.
For example:
public string Format(string input)
{
decimal value = decimal.Parse(input);
return value.ToString("#,#.00");
}
// usage: Format("1000.0")
Example: https://dotnetfiddle.net/P7psPC
However if you're dealing with currency you can just use the C format specifier:
value.ToString("C");
This would output the following for your sample inputs:
$80.00
$1,000.00
$1,000.00
($100.00)
There isn't a real currency converter in Microsoft .Net Framework, so you have to work within the constraints provided, which is decimal. The closest you can get to your decimal being defaulted to money would be:
decimal amount = 0.00m
The m at the end is declaring that this decimal will represent money. Which when added will provide basic rounding as if it were money.
However the easiest approach would be how Andrew Whitaker did the conversion. The only thing I would add, is that when you utilize:
value.ToString("#,##0.00"); // Output: 0.96
value.ToString("#,#.00"); // Output .96
value.ToString("0.##"); // Output 0.6
To clarify the conversion with ToString(); for the decimal.

How to deal with right zeros in a string to decimal conversion?

in my website i need to read data from a XML, and some of these datas are decimal values.
Sometimes the value comes correct: 1 or 72,59and sometimes the value comes like 1.0000 or 72,590000, how is the best way to convert it to the right format:
ps: I need a string value.
Thanks!
What format are you wanting them to go to, specifically? How many decimals, etc?
If you want always 2 decimals, try a standard numeric formatting such as:
decimal value = 123.456;
Console.WriteLine("Your account balance is {0:F}.", value);
See this MSDN example for other common numeric formatting techniques.
You write that you tried
string.Format("{0:F}","1.00000");
The problem with this is that you're passing a string into the function. Numeric formatting only works on numeric data types. Convert the value to a decimal first and then format it.
Try this
public string ReformatDecimalString(string input)
{
const string formatString = //something
var decimalValue = decimal.Parse(input);
return decimalValue.ToString(formatString);
}
When you are formatting a single numeric value, it's slightly more efficient to use x.ToString(formatString) than string.Format(formatString, x). But note that the specific format string will be different in the two cases.
If your input data has decimal points (not commas) and your computer's culture uses decimal commas, you ought to be able to parse the value correctly by using CultureInfo.InvariantCulture:
var decimalValue = decimal.Parse(input, CultureInfo.InvariantCulture);
If I'm reading your answer correctly, you're trying to convert Integer values you pull from an XML file into string values without trailing zeroes ("ps: I need a string value.")
this code:
decimal test = 20.000000m
test.ToString("G29");
might do what you want

Math.Round, keep decimal place [duplicate]

This question already has answers here:
Formatting a float to 2 decimal places
(9 answers)
Closed 9 years ago.
For example.
Math.Round(2.314, 2) //2.31
Math.Round(2.301, 2) //2.3 , but I want this as 2.30
Numbers don't have any conception of zeroes after a decimal point.
You're actually asking how to convert the number into a string with extra zeroes:
(2.301).ToString("0.00") // "2.30"
See numeric format strings for more detail.
In particular, the 0 specifier will round away from zero.
You want a string formatting of the number:
string val = Math.Round(2.301, 2).ToString("F2");
here's a post on formatting numbers in C#
2.3 and 2.30 are the same thing. If you want the string 2.30 then use .ToString("F2") on the Math.Round function.
2.3 and 2.30 is the same thing from a code perspective. You can display the trailing zero by formatting a string:
string yourString = Math.Round(2.301, 3).ToString("0.00");
The decimal is still there, you're probably just not seeing because when you look at the string representation, by default it will omit trailing zeros. You can overwrite this behavior by passing a format string to ToString():
Console.WriteLine(Math.Round(2.301, 2).ToString("N2")) // 2.30
But of course, if this is just for display purposes, you don't really need to call Math.Round:
Console.WriteLine(2.301.ToString("N2")) // 2.30
Further Reading
Standard Numeric Format Strings
Custom Numeric Format Strings
If you use decimal numbers (their literals end with m, for "money"), you get the behavior you're after. double numbers don't have a concept of significant zeroes the same way that decimals do.
Math.Round(2.314m, 2);
Math.Round(2.301m, 2);
Or if you want to change how you see the numbers, you can use a string format:
Math.Round(2.314, 2).ToString("N2");
Math.Round(2.301, 2).ToString("N2");

How do I make sure a decimal is always displayed as xxxx.xxxx positive and negative?

I have a decimal on an object in C#. I want to be able to display it as xxxx.xxxx at the moment the value is -1.61769, basically I want to round the last two digits up and make sure that it only ever has 4 decimal places after the decimal place. Im not sure if this is a Math operation (i.e. Math.Round) or is a validation operation (i.e. string.Format) or both?..
Hope someone can help...
Try this:
string result = d.ToString("0.0000");
You may also want to specify a culture:
CultureInfo cultureInfo = CultureInfo.InvariantCulture;
string result = d.ToString("0.0000", cultureInfo);
Result:
-1.6177
See it working online: ideone
That depends on whether you want at most four decimal places, or exacty four decimal places.
If you round the value, you get at most four decimal places:
value = Math.Round(value, 4);
The value 1.61799 for example would be rounded to 1.6180 and display as 1.618.
If you format the value, you get exactly four decimal places:
string formatted = value.ToString("0.0000");
you have to check with string.Format if you are looking to have a string.
check that link c# double format
if you are looking for keeping the value of the number you should use Math.round
Did you try Math.Abs()?
According to this MSDN-Page it is not possible.
Because it would display a diffierent Value.
Math.Abs(-1.61769).ToString("####.####");

Convert float to string with comma and dot

I have a nullable float. The internal decimal places can be separated with dot or comma e.g. 1.2 or 1,2. I need this float as a string to compare it to a Regex. If I use the Convert.toString method, the float with the comma is 12 and not 1.2. How can I convert a float to String without loosing the comma or the dot? I alredy tried to convert it with diffrent cultures.
Thanks for your help
A solution for this can be the following:
float? num = 1.2f;
string floatAsString = string.Format("{0:f}", num.Value);
Maybe you need to check if the HasValue property is true before you use the value. For more examples: http://alexonasp.net/samples/stringformatting/
String.Format() function with mask. But can you convert your strings to numbers rather than your numbers to strings, for purposes of the comparison? Does it have to be a regex comparison?
Try:
string s = yourFloat.ToString();
Using the invariant culture is recommended if you want to be sure that your output will be in the correct form, but I'd be surprised if there were a culture which doesn't output a comma or a dot.
I would also suggest not using regular expressions to validate the value of a float.
Are you certain that the textbox allows both "." and "," as a decimal-separator (as opposed to a grouping character, also known as a thousands-separator)?
When you are certain that you only get decimal separators and no grouping characters, replace any "," with a "." before using TryParse with an InvariantCulture to convert the string to a float.
OR use the same culture in the code as on the client side, so both will use the same decimal separators.
As others mentioned, a float doesn't have the concept of various decimal separators.
Ok I solved the problem. I did in my xaml a converter which only allows to enter values with commas as separator, so I dont need any checks if there are only two internal decimal places. Thanks for your help
If it's a WinForm Application, there's a static variable Application.CurrentCulture.NumberFormat.NumberDecimalSeparator.
Depending on it's value you get different results when converting ToString().
Try manipulating this parameter to achieve necessary result.

Categories