0

I am trying to replace a comma in a string.

For example, the data is the currency value of a part.

Eg. 453,27 This is the value I get from an SAP database

I need to replace the comma to a period to fix the value to the correct amount. Now sometimes, it will be in the thousands.

Eg. 2,356,34 This value needs to be 2,356.34

So, I need help manipulating the string to replace comma that is 2 characters from the end.

Thanks for the help

ToolmakerSteve
  • 18,547
  • 14
  • 94
  • 196
tluck234
  • 71
  • 1
  • 3
  • 10
  • 3
    Why should a number like 2,356,34 be stored in that way if a comma is meant to either be a thousand separator OR a decimal separator? That looks very odd to me – Peter Monks Jun 15 '12 at 12:49
  • 1
    Also you should really show us what you've already tried – Peter Monks Jun 15 '12 at 12:51
  • It is just the way it is coming from the database. The database saves it like that, and somehow it understands it. It did not make sense to me either. – tluck234 Jun 15 '12 at 12:51
  • Possible duplicate of [How to replace part of string by position?](http://stackoverflow.com/questions/5015593/how-to-replace-part-of-string-by-position) – Tot Zam Apr 10 '17 at 13:35

6 Answers6

1
string a = "2,356,34";
int pos = a.LastIndexOf(',');
string b = a.Substring(0, pos) + "." + a.Substring(pos+1);

You'll need to add a bit of checking for cases where there are no commas in the string, etc. but that's the core code.

You could also do it with a regex, but this is simple and reasonably efficient.

Jason Williams
  • 56,972
  • 11
  • 108
  • 137
  • Just put this in. Ran like a dream. No more errors when adding to the database. The LastIndex was the key to it. I had tried something close to this, but could not figure out how to get it in that position. Thank you very much. Will mark as answer soon – tluck234 Jun 15 '12 at 12:54
  • This is very inefficient – Jonny Jun 15 '12 at 13:01
0

A quick google search gave me this:

void replaceCharWithChar(ref string text, int index, char charToUse)
{
    char[] tmpBuffer = text.ToCharArray();
    buffer[index] = charToUse;
    text = new string(tmpBuffer);
}

So your "charToUse" should be '.'. If it always is 2 characters from end, your index should be text.length - 3.

http://www.dreamincode.net/code/snippet1843.htm

Lucas Arrefelt
  • 3,879
  • 5
  • 41
  • 71
0

Use this :

string str = "2,356,34";
string[] newStr = str.Split(',');
str = string.Empty;
for (int i = 0; i <= newStr.Length-1; i++)
{
    if (i == newStr.Length-1)
    {
        str += "."+newStr[i].ToString();
    }
    else if (i == 0)
    {
        str += newStr[i].ToString();
    }
    else
    {
        str += "," + newStr[i].ToString();
    }
}
string s = str;
slavoo
  • 5,798
  • 64
  • 37
  • 39
Sunny
  • 3,185
  • 8
  • 34
  • 66
0

If I understand correctly, you always need to replace the last comma with a period.

public string FixSAPNumber(string number)
{
    var str = new StringBuilder(number);
    str[number.LastIndexOf(',')] = '.';
    return str.ToString();
}
Allon Guralnek
  • 15,813
  • 6
  • 60
  • 93
0
string item_to_replace = "234,45";

var item = decimal.Parse(item_to_replace);

var new_item = item/100;

//if you need new_item as string 
//then new_item.ToString(Format)
Rohan Kandwal
  • 9,112
  • 8
  • 74
  • 107
IrisA
  • 1
  • 1
-1
string x = "2,356,34";
if (x[x.Length - 3] == ',')
{
    x = x.Remove(x.Length - 3, 1);
    x = x.Insert(x.Length - 2, ".");
}
slavoo
  • 5,798
  • 64
  • 37
  • 39
Jonny
  • 2,787
  • 10
  • 40
  • 62
  • Realized that the thousand numbers also use a period for the comma. Used your code to change both problems to the correct format. – tluck234 Jun 15 '12 at 13:06
  • @tluck234 - that makes more sense then. You received *European* format, are attempting to read its value, but your parsing is set to *American* culture. An alternative fix would have been to *set the culture to European* at the time you parsed it. [I don't know the exact details, but look up "culture" / "thousands separator" / "decimal point"]. – ToolmakerSteve Apr 08 '17 at 21:04