0
String num = "93456"

decimal decinum = Convert.ToDecimal(num);
var newnum = decinum.ToString();

I need to convert string value into decimal value and back to string value

For instance, 93456 string format in to 93.456 decimal format then back to 93.456 string format.

I need to place a decimal after two fist digits of a string.

2

6 Answers 6

2

Here's my attempt:

String myString = "93456";

if (myString.Length > 2)
{
    myString = myString.Insert(2, Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator);
}

var myDecimal = decimal.Parse(myString);

Console.WriteLine(myDecimal);
Console.WriteLine(myDecimal.ToString());
Sign up to request clarification or add additional context in comments.

Comments

0

You can try this

string num = TextBox1.Text; //"93456.12";
if (num.Length > 2)
{
    decimal decNumber = Convert.ToDecimal(num.Substring(0, 2) + "." + num.Substring(2, num.Length - 2).Replace(".",""));
     string strNumber = Convert.ToString(decNumber);
}

Comments

0

This may help you:

string num1 = "93456";
int value = Convert.ToInt32(num1);
int digits = (num1.Length - 2) > 0 ? (num1.Length - 2) : 0;
decimal number1 = value / Convert.ToDecimal(Math.Pow(10, digits));
string result = number1.ToString();

Comments

0

Yet another way:

string num = "93456";

decimal decinum = decimal.Parse(num);

string newnum = decinum.ToString("#,#", new NumberFormatInfo { NumberGroupSeparator = "." });

Comments

0
string num = "9312";
string newNum = num.Length > 2 ? num.Insert(2, ".") : num + ".000";
decimal newDecimal; 

if (Decimal.TryParse(newNum, out newDecimal))
    Console.WriteLine(newDecimal);
else
    Console.WriteLine("not a valid decimal '{0}'.", newDecimal);      

1 Comment

This is missing the 2nd part of the task.
0
decimal num = 93456;

StringBuilder sb = new StringBuilder(num.ToString());

if (sb.Length > 2)
    sb.Insert(2, Thread.CurrentThread.CurrentCulture
                .NumberFormat.CurrencyDecimalSeparator);

Console.WriteLine(sb);

num = decimal.Parse(sb.ToString());

Console.WriteLine(num);

Update we don't have alot of string manipulation,so don't need using StringBuilder. also according the question we should use NumberDecimalSeparator instead of CurrencyDecimalSeparator

decimal num = 93456;

string str = num.ToString();

if (str .Length > 2)
    str .Insert(2, Thread.CurrentThread.CurrentCulture
                .NumberFormat.NumberDecimalSeparator);

Console.WriteLine(str);

num = decimal.Parse(str);

Console.WriteLine(num);

2 Comments

Why the StringBuilder? Also, why CurrencyDecimalSeparator and not NumberDecimalSeparator?
StringBuilder performance is better because string type is immutable,also we don't have alot of manipulation in string and not matter using string or StringBuilder.but using StringBuilder is a good habit.about NumberDecimalSeparator, you are right,it's better

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.