Format custom numeric string with fixed character count - c#

I need to convert any number in a fixed format with a fixed amount of characters. Means 1500 and -1.5 or 0.025 need to have the same length. I also have to give the format in this form: Format = "{???}";
When i type Format = "{0000}"; i can limit 1500 to "1500", but -1.5 -> "-0001.5" means i have too much numbers after the point.
Negative sign place can be done with Format = "{ 0.0;-0.0; 0.0}".
How can i fix the count of the numbers for different numbers?
The length of the string doesn't matter, the most important is the equal length.
Examples:
1500 -> " 1500.000" or " 1500"
-1500 -> "-1500.000" or "- 1500" or " -1500"
1.5 -> " 1.500" or " 1.5"
-0.25-> " -0.250" or "- 0.25"
0.00005 -> " 0.000" or " 0"
150000-> " 150000.0" or " 150000"
15000000 " 15000000"
Edit:
I want to Format an y-Axis of a Chart. I can't use something like value.ToString("???") i need to use chartArea.AxisY.LabelStyle.Format = "{???}";

Why don't use formatting? "F3" forces 3 digits after decimal point and PadLeft ensures the overall length
Double value = 1500.0;
// 3 digits after decimal point, 9 characters length
String result = value.ToString("F3").PadLeft(9, ' ');
0 -> 0.000
1500.0 -> 1500.000
-1500.0 -> -1500.000
-0.25 -> -0.250
Another (similar) possibility is String.Format:
Double value = 1500.0;
// Put value at place {0} with format "F4" aligned to right up to 9 symbols
String result = String.Format("{0:9,F4}", value);

Try it > result = Math.Round(yourValue, 3);
Check full reference here !

you cannot achieve this by a simple format function
string result = string.Empty;
var array = dec.ToString().Split('.');
if (dec > 0)
{
result = array[0].PadLeft(9).Remove(0, 9);
if (array.Count() > 1)
{
result += '.' + array[1].PadRight(3).Remove(3);
}
}
else
{
result = "-"+array[0].PadLeft(9).Remove(0, 9);
if (array.Count() > 1)
{
result += '.' + array[1].PadRight(3).Remove(3);
}
}

Related

How to truncate a number to a fixed size without rounding when displaying it with Console.WriteLine()

I'm trying to format a number in a very specific way when displaying it, I've tried messing around with String.Format but couldn't find a way to make this work exactly as I needed.
For example, let's way that I have a variable of type Double with the value "123459.7889" and that I want to format it as "459.788" (000.000) but only during Console.WriteLine() and without rounding or changing the internal value of the number. The full value of the number would not show up and it should technically "overflow" the string because it should only print the first 3 numbers to the left of the decimal point.
Here are some example inputs and example outputs:
12345.678 formatted to "000.000" would become 345.678
1000000.678 formatted to "000.000" would become 000.678
1.777888 formatted to "000.000" would become 001.777
99999.9 formatted to "000.000" would become 999.900
Basically, no matter the internal size of the number, the formatted output should always be the 3 numbers to the left of the decimal point and the 3 numbers to the right of the decimal point with zeros to replace the missing spaces if there's any.
I've tried looking at the examples on the C# documentation and found this:
double value = 123459.7889;
string result = String.Format("{0,8:00000000} {0,8:00000000.000}", value);
Console.WriteLine(result);
But it doesn't work exactly like I needed it to. When running it the numbers are rounded so it becomes 00123459.789 instead of 00123459.788 for example and if the number grows it no longer stays at that fixed size.
try this:
double value = 123459.7889;
Console.WriteLine((Math.Truncate(Math.Abs(value) % 1000) + double.Parse((Math.Round(Math.Abs(value), 3, MidpointRounding.ToZero) -
Math.Truncate(Math.Abs(value))).ToString(".000"))).ToString("000.000"));
value= 123459.7889; result=345.678
value= 1000000.678; result=000.678
value= 1.777888; result=001.777
value= 99999.9; result=999.900
value= 152; result=152 .000
value= -100.02; result=100.020
value= 10.0005; result=010.000
We can use PadLeft and PadRight to fill up zeros when value characters are not enough.
Implementation:
static void Main(string[] args)
{
double value = 1.12;
string valueStr = value.ToString().Replace("-", ""); // convert to string
// check if value does not contain decimal and add
if (!valueStr.ToString().Contains("."))
valueStr += valueStr + ".0"; // add decimal part
var arr = valueStr.ToString().Split(".");
string intValue = arr[0]; //1
intValue = intValue.PadLeft(3,'0'); //001
intValue = intValue.Substring(intValue.Length - 3, 3); //001
string decimalValue = arr[1]; //12
decimalValue = decimalValue.PadRight(3,'0'); //120
decimalValue = decimalValue.Substring(0, 3); //120
string result = $"{intValue}.{decimalValue}";
Console.WriteLine(result);
}
Result:
12345.678 > 345.678
1000000.678 > 000.678
1.777888 > 001.777
99999.9 > 999.900
152 > 152.000
-100.02 > 100.020
-10.0005 > 010.000
You shouldn't need to convert to string, nor should you rely on splitting on a character that may or may not be the decimal character in the language you are running. You can do all the calculations on the numbers and then format the output without allocating any strings except for the result.
public static string FormatNumber(double number)
{
int first = ((int)number)%1000;
int second = (int)Math.Truncate((number-(int)number)*1000)%1000;
return $"{first:000}.{second:000}";
}
static string Format(double number)
{
return string.Format("{0:N3}", number % 1000).PadLeft(7,'0');
}
How it works:
Remove the upper digits using modulus (%)
Format to three decimal places using a format string
Pad left using PadLeft()
Full example:
using System;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
var tests = new Dictionary<double,string>
{
{ 12345.678D, "345.678" },
{ 1000000.678, "000.678" },
{ 1.777888, "001.778" },
{ 99999.9, "999.900"}
};
foreach (var pair in tests)
{
Console.WriteLine("Expected: {0} Actual: {1}", pair.Value, Format(pair.Key));
}
}
static string Format(double number)
{
return string.Format("{0:N3}", number % 1000).PadLeft(7,'0');
}
}
Output:
Expected: 345.678 Actual: 345.678
Expected: 000.678 Actual: 000.678
Expected: 001.778 Actual: 001.778
Expected: 999.900 Actual: 999.900
Fiddle
try this
int i = value.ToString().IndexOf(".");
string result = string.Concat(value.ToString().Substring(i - 3, 3),".", value.ToString().Substring(i + 1, 3));
Console.WriteLine(result);

Add/remove to decimal places for greater and smaller sign

I want to convert string values to decimal. When there is a smaller or greater symbol I want to add/remove to the value like this
string value | decimal result
--------------|----------------
"< 0.22" | 0.219
"< 32.45" | 32.449
"> 2.0" | 2.01
"> 5" | 5.1
This has to work for decimal numbers with any number of decimal places. Is there an elegant way to do this?
I can only think of counting the number of decimal places, getting the last digit, adding/removing ...
So I would imagine the following solution
Split the string on the space
Identify the sign (GT or LT)
Count the number of decimal places and store that value
Convert the number to a decimal
Based on the symbol either add or subtract 10^(-1 * (numberOfDecimals + 1))
Using Regex
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string[] inputs = {
"< 0.22",
"< 32.45",
"> 2.0"
};
string pattern = #"(?'sign'[\<\>])\s+(?'integer'\d+).(?'fraction'\d+)";
decimal number = 0;
foreach(string input in inputs)
{
Match match = Regex.Match(input, pattern);
if(match.Groups["sign"].Value == ">")
{
number = decimal.Parse(match.Groups["integer"].Value + "." + match.Groups["fraction"].Value);
number += decimal.Parse("." + new string('0', match.Groups["fraction"].Value.Length) + "1");
}
else
{
number = decimal.Parse(match.Groups["integer"].Value + "." + match.Groups["fraction"].Value);
number -= decimal.Parse("." + new string('0', match.Groups["fraction"].Value.Length) + "1");
}
Console.WriteLine(number.ToString());
}
Console.ReadLine();
}
}
}
Solution without counting the decimals:
If the string starts with >, append "1". If the string starts with < replace the last character by the next lower one (Notice: for "0"->"9" and repeat on the character left to it) and then append "9".
Then split at the blank and Double.Parse the right part.
No need to count digits or split apart the decimal... (you can run this in linqpad)
string inputString = "> 0.22";
string[] data = inputString.Split(' ');
double input = double.Parse(data[1]);
double gt = double.Parse(data[1] + (data[1].Contains('.') ? "1" : ".1"));
switch(data[0])
{
case ">":
gt.Dump(); // return gt;
break;
case "<":
double lt = input - (gt - input);
lt.Dump(); // return lt;
break;
}
// you could even make it smaller by just doing (instead of the switch):
return data[0]==">" ? gt : input - (gt - input);
If trailing zeros are allowed as input but are considered insignificant then trim them.
inputString = inputString.TrimEnd("0");

exporting datagridview to textfile with syntax error for converting money to string format

I am trying to convert money to string like claim amount = 100.00 should be converted to 0010000
Court fees = 15 converted to 01500 and solictors fee = 00000(always the same number)
and total amount = 115 converted to 00011500. I dont how to convert these to zeros in the first place.
string value = Convert.ToString(ReturnValue);
Gives output :it is showing as 100.0000
can you help me where i am going wrong.
I tried this but still the same result. it is an sql query
" bat.PCN_Charge *100 ".ToString().PadLeft(7, '0') +
",[Court Fee] *100 ".ToString().PadLeft(5, '0') +
",[Solictors Fees] *100 ".ToString().PadLeft(5, '0') +
", (bat.PCN_Charge + [Court Fee]) *100".ToString().PadLeft(8, '0') +
My results are like these 10000.0000 1500 0 11500.0000
If you know your string should always have a length of seven, you can first calculate the plain int-value, then use:
value.ToString().PadLeft(7, '0')
// Example
var numericValue = 100.00;
var intValue = numericValue * 100; // 10000
var paddedResult = intValue.ToString().PadLeft(7, '0'); // 0010000
Alternatively, you can find lot's of info about padding numbers with zero here.
yes very simple
First multiply by 100 to override the decimal point.
Second use Format instead of convert
Try this
string value = string.Format("{0:0000000}", ReturnValue * 100);
Happy Coding
:)

Format decimal value to string with leading spaces

How do I format a decimal value to a string with a single digit after the comma/dot and leading spaces for values less than 100?
For example, a decimal value of 12.3456 should be output as " 12.3" with single leading space. 10.011 would be " 10.0". 123.123 is "123.1"
I'm looking for a solution, that works with standard/custom string formatting, i.e.
decimal value = 12.345456;
Console.Write("{0:magic}", value); // 'magic' would be a fancy pattern.
This pattern {0,5:###.0} should work:
string.Format("{0,5:###.0}", 12.3456) //Output " 12.3"
string.Format("{0,5:###.0}", 10.011) //Output " 10.0"
string.Format("{0,5:###.0}", 123.123) //Output "123.1"
string.Format("{0,5:###.0}", 1.123) //Output " 1.1"
string.Format("{0,5:###.0}", 1234.123)//Output "1234.1"
Another one with string interpolation (C# 6+):
double x = 123.456;
$"{x,15:N4}"// left pad with spaces to 15 total, numeric with fixed 4 decimals
Expression returns: " 123.4560"
value.ToString("N1");
Change the number for more decimal places.
EDIT: Missed the padding bit
value.ToString("N1").PadLeft(1);
Many good answers, but this is what I use the most (c# 6+):
Debug.WriteLine($"{height,6:##0.00}");
//if height is 1.23 => " 1.23"
//if height is 0.23 => " 0.23"
//if height is 123.23 => "123.23"
All above solution will do rounding of decimal, just in case somebody is searching for solution without rounding
decimal dValue = Math.Truncate(1.199999 * 100) / 100;
dValue .ToString("0.00");//output 1.99
Note the "." could be a "," depending on Region settings, when using string.Format.
string.Format("{0,5:###.0}", 0.9) // Output " .9"
string.Format("{0,5:##0.0}", 0.9) // Output " 0.9"
I ended up using this:
string String_SetRPM = $"{Values_SetRPM,5:##0}";
// Prints for example " 0", " 3000", and "24000"
string String_Amps = $"{(Values_Amps * 0.1),5:##0.0}";
// Print for example " 2.3"
Thanks a lot!

Add one space after every two characters and add a character infront of every single character

I want to add one space after every two characters, and add a character in front of every single character.
This is my code:
string str2;
str2 = str1.ToCharArray().Aggregate("", (result, c) => result += ((!string.IsNullOrEmpty(result) && (result.Length + 1) % 3 == 0) ? " " : "") + c.ToString());
I have no problems separating every two characters with one space, but how do I know if the separated string has an individual character, and add a character infront of that character?
I understand that my question is confusing as I'm not sure how to put what I want in words..
So I'll just give an example:
I have this string:
0123457
After separating every two characters with a space, I'll get:
01 23 45 7
I want to add a 6 infront of the 7.
Note: Numbers are dependent on user's input, so it's not always the same.
Thanks.
[TestMethod]
public void StackOverflowQuestion()
{
var input = "0123457";
var temp = Regex.Replace(input, #"(.{2})", "$1 ");
Assert.AreEqual("01 23 45 7", temp);
}
Try something like this:
static string ProcessString(string input)
{
StringBuilder buffer = new StringBuilder(input.Length*3/2);
for (int i=0; i<input.Length; i++)
{
if ((i>0) & (i%2==0))
buffer.Append(" ");
buffer.Append(input[i]);
}
return buffer.ToString();
}
Naturally you'd need to add in some logic about the extra numbers, but the basic idea should be clear from the above.
May be you can try, if i right understand your request,
String.Length % 2
if result is 0, you done with first iteration, if not, just add a character infront of last one.
I think this is what you asked for
string str1 = "3322356";
string str2;
str2 = String.Join(" ",
str1.ToCharArray().Aggregate("",
(result, c) => result += ((!string.IsNullOrEmpty(result) &&
(result.Length + 1) % 3 == 0) ? " " : "") + c.ToString())
.Split(' ').ToList().Select(
x => x.Length == 1
? String.Format("{0}{1}", Int32.Parse(x) - 1, x)
: x).ToArray());
result is "33 22 35 56"

Categories

Resources