So I have kinda helper with this code which I use everywhere in system where I need to round a double:
public static double Round(double source)
{
return Math.Round(source, Program.AppSettings.DigitAfterComma);
}
The idea is to round any input double to double with some character after comma which is read from file. I use it in my services with calculations and in my ViewModels and .cshtml for results render.
The problem is that dealing with calculations is OK, but when I need to render doubles like 15.0% I get only 15% as output. It will be hard to write 2 methods for renderer and for calculations because this method has a numerous references all over the system.
Is there any way to get xx.0 everytime I call the method without formatting it to string because I need double type output for calculations?
Related
Our goal is to build a toy abstract syntax tree for C# classes using Roslyn. We just want to show the basic structure of a class instead of walking through the entire AST. For example (Taken from MSDN):
class TimePeriod
{
private double seconds;
public double Hours
{
get { return seconds / 3600; }
set { seconds = value * 3600; }
}
}
Let us only consider the Property Hours; we are only interested in extracting the tokens for modifier (public), return type (double), identifier (Hours) as for the body of two accessors we want to directly extract it as a String.
However, as we walk though the roslyn (shown in the screen dump) when we get to get accessor's body we did not find the field representing the entire string. What's the correct way of achieving this?
The obvious way is to call ToString:
Returns the string representation of this node, not including its leading and trailing trivia.
If you want the leading and trailing trivia (whitespace, comments, ...), there's ToFullString:
Returns full string representation of this node including its leading and trailing trivia.
For efficiency purposes, you may also be interested in the WriteTo method, which writes what ToFullString would produce to a TextWriter, avoiding intermediate string allocations:
Writes the full text of this node to the specified TextWriter.
I want get full double value in c#, for EX:
double a = 984554546543213213215465879875645432132112548787;
MessageBox.Show(a.ToString());
//get: 984554546543213213215465879875645432132112548787
but get 9.84554546543213E+47
I use DoubleConverter but its not working, its get:
//984554546543213265388222902015563129359765078016
Doubles simply don't have enough accuracy for this application. It's not a problem with the displaying of the value; it's a problem with how a double is actually stored and how it's inherently approximate.
Based on the example given, you might want to take a look a the BigInteger Structure.
decimal might also be worth looking at.
I've run into an unusual quirk in a program I'm writing, and I was trying to figure out if anyone knew the cause. Note that fixing the issue is easy enough. I just can't figure out why it is happening in the first place.
I have a WinForms program written in VB.NET that is displaying a subset of data. It contains a few labels that show numeric values (the .Text property of the labels are being assigned directly from the Decimal values). These numbers are being returned by a DLL I wrote in C#. The DLL calls a webservice which initially returns the values in question. It returns one as a string, the other as a decimal (I don't have any control over the webservice, I just consume it). The DLL assigns these to properties on an object (both of which are decimals) then returns that object back to the WinForm program that called the DLL. Obviously, there's a lot of other data being consumed from the webservice, but no other operations are happening which could modify these properties.
So, the short version is:
WinForm requests a new Foo from the DLL.
DLL creates object Foo.
DLL calls webservice, which returns SomeOtherFoo.
//Both Foo.Bar1 and Foo.Bar2 are decimals
Foo.Bar1 = decimal.Parse(SomeOtherFoo.Bar1); //SomeOtherFoo.Bar1 is a string equal to "2.9000"
Foo.Bar2 = SomeOtherFoo.Bar2; //SomeOtherFoo.Bar2 is a decimal equal to 2.9D
DLL returns Foo to WinForm.
WinForm.lblMockLabelName1.Text = Foo.Bar1 //Inspecting Foo.Bar1 indicates my value is 2.9D
WinForm.lblMockLabelName2.Text = Foo.Bar2 //Inspecting Foo.Bar2 also indicates I'm 2.9D
So, what's the quirk?
WinForm.lblMockLabelName1.Text displays as "2.9000", whereas WinForm.lblMockLabelname2.Text displays as "2.9".
Now, everything I know about C# and VB indicates that the format of the string which was initially parsed into the decimal should have no bearing on the outcome of a later decimal.ToString() operation called on the same decimal. I would expect that decimal.Parse(someDecimalString).ToString() would return the string without any trailing zeroes. Everything I find online seems to corroborate this (there are countless Stack Overflow questions asking exactly the opposite...how to keep the formatting from the initial parsing).
At the moment, I've just removed the trailing zeroes from the initial string that gets parsed, which has hidden the quirk. However, I'd love to know why it happens in the first place.
It's because the scaling factor also preserves any trailing zeros in a Decimal number. Trailing zeros do not affect the value of a Decimal number in arithmetic or comparison operations. However, trailing zeros might be revealed by the ToString method if an appropriate format string is applied.
It's just few days ago that I jumped into learning C# and I already have one problem with understanding basics.. Maybe it's just the language barrier (I'm not English native speaker). Please, could you explain me how to understand parsing? For example: while creating a very simple calculator I wanted to read the first input number (which is a variable a). I use this code:
float a = float.Parse(Console.ReadLine());
and the same with b for the other number:
float b = float.Parse(Console.ReadLine());
I learnt that the float is a data type for decimals numbers so what exactly does this particular Parse() stands for?
Obviously, I tried to run the application without parsing and it wouldn't work because it reads it as string, but why? Thank you..
Console.ReadLine() returns a string, which represents a piece of text. So, from the computer's point of view, what you have after calling Console.ReadLine() is a piece of text. It may or may not contain the text "6.0", but from the computer's point of view, it is just a piece of text. As such, you cannot use it to add, subtract etc.
Using the float.Parse(...) method, you tell the computer: "This piece of text actually represents a floating point number, could you please read the text and give me back a number, so that I can start doing math with it?".
The method you are using, float.Parse() is just one of many such methods that take a String input value, and attempt to convert it into the target type, here a float.
There is a safer alternative, however, and it is TryParse():
float a;
if (float.TryParse(Console.ReadLine(), out a))
{
//do something with your new float 'a'
}
In either case, your are asking the framework to inspect the value you provide, and attempt to make a conversion into the requested type. This topic can be quite deep, so you'll want to consult MSDN for the specifics.
Console.ReadLine reads text that the user inputs and returns it to the program so that you may do with it what you want. Therefore, the ReadLine method returns a string.
If you want to work with a decimal (check the decimal class instead of float), you need to convert the string, which is a character sequence, to a number of your desired type, that's where float.Parse comes in:
float.Parse accepts a string and if possible, returns a float value.
Almost every type contains the Parse method which is used to transform a string into the calling one.
I'm working in C# and trying to parse out the floats on each side of the "w" from the following string:
"10.3w20.5"
I want to have the floats available to use as floats in an equation. How do I accomplish this?
I tried splitting the string by length but then realized that the values on each side could be variable in length.
How about:
var floats = "10.3w20.5".Split('w').Select(s => Convert.ToSingle(s));
The only thing to add to avalable answers, is what usually people forget to mantion in conversion mnagement. If you're going to operate in multiculture environment, pay attention on Culture you use to store and convert data to.
public static float ToSingle(
string value,
IFormatProvider provider
)
I would say, even if you're not going to operate in multiculture environment, it's always a good to pay attention on this.