0

I have a string "123456" and I want to make it "123.456". Is there a function in c# that converts the value to what I need?

Other examples: "1000000" -> "1.000.000"

Dev Db
  • 740
  • 4
  • 10
  • 30
  • If you had a number you could use [this](https://stackoverflow.com/questions/3456926/how-to-insert-a-thousand-separator-comma-with-convert-to-double). – ProgrammingLlama May 30 '18 at 12:25
  • 2
    What you have is a string. If what you want is an `int` or a `long` or something of that nature then you can use `int.TryParse()` or `long.TryParse()` or something of that nature. – David May 30 '18 at 12:25
  • Is that `.` a thousands separator in your example? – Ben Voigt May 30 '18 at 12:25
  • 2
    Possible duplicate of [.NET String.Format() to add commas in thousands place for a number](https://stackoverflow.com/questions/105770/net-string-format-to-add-commas-in-thousands-place-for-a-number) – Milster May 30 '18 at 12:26
  • It is a thoousand separator indeed, but with a point – Dev Db May 30 '18 at 12:28
  • 1
    convert to long and then use the answer in the suggested duplicate – Zohar Peled May 30 '18 at 12:29
  • 1
    Parse it to integer and then use ToString with the desired [format string](https://msdn.microsoft.com/de-de/library/0c899ak8(v=vs.85).aspx) – Tim Schmelter May 30 '18 at 12:30

2 Answers2

5

First parse it to numeric type (depending what would be the length of your string), for example lets use long (and long.TryParse() method):

string str = "1000000";
long num = 0;
long.TryParse(str, out num); //or long.TryParse(str, out long num); in c# 7

then use ToString() to convert it back to string using specified format:

string nstr = num.ToString("N0");

Some article about numeric string formats: Standard Numeric Format Strings

SᴇM
  • 7,024
  • 3
  • 24
  • 41
  • 1
    Do not use `TryParse`, if you ignore its return value. There is a `Parse` method for such case. – Uladzislaŭ May 30 '18 at 12:44
  • 4
    @Vladislav: Unless you're not interested in the return value, but are interested in quietly ignoring invalid input. `TryParse` is a perfectly valid tool to use. – David May 30 '18 at 12:45
  • 1
    @David ignoring its return value is not correct, until you don't want to fallback to some default value. And you don't have meaningful defaults in the scenario discussed. – Uladzislaŭ May 30 '18 at 12:51
  • 4
    @Vladislav: *"ignoring its return value is not correct"* - Perhaps not in *your* use cases. But there exist use cases outside of *your* use cases. The OP isn't asking about defaults or handling invalid input. That's up to *his* use cases. `TryParse` is a tool. And like any tool, it has valid uses. This is one of them. Just because something isn't correct *for your needs* doesn't mean it isn't correct *for all needs*. – David May 30 '18 at 12:54
  • @David this is not about *my* use cases, this is about advices considering correctness. – Uladzislaŭ May 30 '18 at 17:41
  • @Vladislav I don't think, that not using method's return value is something incorrect. I can bring lot of examples, when return value of method doesn't matter, it depends on situation. I'm not familiar with OP's situation, I don't know how will he get that string numbers, what would be that numbers (int, double, float), how will he use them after, but in fact I gave him some sort of example (also link to `TryParse` MSDN page), so he can decide what to do next. – SᴇM May 31 '18 at 05:22
0

If you want to do it by just making a call to a function :

private string AddSeparator(string value)
{
   string newvalue = string.Empty;

   try
   {
      newvalue = Convert.ToInt32(value).ToString("N");
   }
   catch
   {
      //Do whatever you want if conversion fails
   }

   return newvalue;
}
Noob dev
  • 121
  • 10
  • `Convert` class should be used for conversions only if types are not known at compile time. For other cases there is a `Parse` method. `Int32.Parse` for example. – Uladzislaŭ May 30 '18 at 12:47