Get net vat values the vat value in C# - c#

I need to get the net values (vat #20%) of wallet and paper as below, added together
{
this.transaction.netValue = Math.Round(Convert.ToDecimal(wallet) + Convert.ToDecimal(paper), 2);
}
and then to get the vat value (20%) having established the net values of both wallet and paper
{
transaction.vatAmount = Round(Convert.ToDecimal(wallet) + Convert.ToDecimal(paper), 2);
}
I know how to do the formulas in excel, but having difficulty in c#
Updated
this.transaction.netValue = Math.Round(Convert.ToDecimal([wallet]/1.2m) + Convert.ToDecimal([paper]/1.2m), 2);

Convert to decimal first then divide by 1.2m

You are seeing squiggly lines because your are trying to divide two different types of datatypes. first your're converting wallet to decimal and then you're dividing it with 1.2 which is double and you need to change it to decimal as well. From decimal (C# Reference)
If you want a numeric real literal to be treated as decimal, use the
suffix m or M. Without the suffix m, the number is treated as a double
and generates a compiler error.
So your final code will be something as follows
this.transaction.netValue = Math.Round(Convert.ToDecimal(wallet) / 1.2m + Convert.ToDecimal(paper) / 1.2m, 2);

Assuming that the values in wallet and paper strings are actually valid decimal numbers then you need to apply
decimal totalValue = Convert.ToDecimal(wallet) +
Convert.ToDecimal(paper);
decimal netValue = totalValue / 1.2m;
this.transaction.netValue = Math.Round(netValue, 2);
Do not put square brackets around the string and do not put the division inside the parameter (a string is required) that you pass to Convert.ToDecimal
Of course this could be written in a single line but because you are recalculating again that netValue for vatAmount then using a temporary variable is better because you can reuse it for the subsequent calculation
(By the way vatAmount = totalValue - netValue, right? )
Instead if your inputs are not guaranteed to be correct decimal values then you should use decimal.TryParse
decimal walletValue;
decimal paperValue;
if(!decimal.TryParse(wallet, out walletValue))
{
MessageBox.Show("Not a valid decimal value for wallet");
return;
}
if(!decimal.TryParse(paper, out paperValue))
{
MessageBox.Show("Not a valid decimal value for paper");
return;
}
decimal totalValue = walletValue + paperValue;
decimal netValue = totalValue / 1.2m;
this.transaction.netValue = Math.Round(netValue, 2);
this.transaction.vatValue = Math.Round(totalValue - vatValue, 2);

Related

How to add precision to decimal value

I want to add precision to the decimal value. For example, I have this value:
decimal number = 10;
I want to make it 10.00. I don't want to convert it to string like number.ToString("#.00")
Currently, I have this method:
decimal CalculatePrecision(decimal value, int precision)
{
var storedCalculated = decimal.Divide(1, Convert.ToDecimal(Math.Pow(10, precision)));
return value + storedCalculated - storedCalculated;
}
Is there any good solution for this?
You can't. 10 and 10.00 are the same number. Only the "presentation" is different. Both "presentations" are strings. The actual number look different. If you need to change the presentation, convert to string.
How about
decimal d = 10;
d += 0.00M;
Console.WriteLine(d);
Try reference
Math.Round not keeping the trailing zero
How do I display a decimal value to 2 decimal places?

C# Math.Round very long double - not working

here is what I'm trying to do:
double result = Math.Pow((1 + 8), 60) - 1;
And the result variable is:
1.7970102999144311E+57 double
And trying to:
Math.Round(result, 5);
Returns same : 1.7970102999144311E+57 double
I'd like to round it to 1.79701 for example
Any solutions ?
You're misunderstanding what you're seeing.
1.7970102999144311E+57
Is scientific notation for
1797010299914431100000... (with 41 trailing zeros).
It is a whole number, thus rounding it to 5 decimal places will correctly return the same value.
What you want to do is format the output of the number
String.Format(CultureInfo.InvariantCulture, "{0:0.#####E+0}", result);
Which returns 1.79701E+57. Note that this is a very different number from 1.79701
The problem you're having is that Math.Round rounds things to the right of the decimal point. For example if you're dealing with currency and you perform an operation that leaves you with $1.5234524, you would use:
Math.Round(1.5234524,2);
// output 1.52
The number you're dealing with is actually scientific notation for a very large number with nothing to the right of the decimal point. This is why the result of Math.Round is the same as the input.
The earlier comments and answers are correct. But to get what you are trying to achieve you can use the following:
double result = Math.Pow((1 + 8), 60) - 1;
string s = String.Format("{0:E5}", result);
double d = Double.Parse(s);

C# precise float variable

I have written one program,it use float variables and after calculate some functions it doesn’t have sufficient precise result.
my program is
int x=0;
ts_sec = 1338526801;
ts_usec = 113676;
while(ir<2)
{
Console.Write("ts_sec:" + ts_sec+"\t");
Console.Write("ts_usec:" + ts_usec+"\t");
if (x == 0)
{
floatstart = ts_sec + ts_usec / 1000000;
Console.Write("startTime" + floatstart+"\t");
x=x+1; //x=1
}
timeStamp = ts_sec + ts_usec / 1000000 - floatstart; //
Console.Write("timestamp is" + timeStamp+"\n");
ts_sec = 1338526801;
ts_usec = 113676;
ir++;
}
this is my out put:
ts_sec:1338526801 ts_usec:113676 startTime1.338527E+09
timestamp is0 ts_sec:1338526801 ts_usec:113678
timestamp is0
but I want my output will be like this and I want my result doesn’t have E .
ts_sec:1338526801 ts_usec:113676 startTime:1338526801.11368
timeStamp:0 ts_sec:1338526801 ts_usec:113678
timeStamp:1.9073486328125e-006
First, change float (32-bit) to double (64-bit) to increase precision nearly two times; second if you don't want the result being in scientific representation (i.e. with "e") choose appropriate formatting:
Double startTime = ...
// Assuming that you want 5 digits after the decimal point
String result = startTime.ToString("F5");
If you want more precision use decimal.
The decimal keyword indicates a 128-bit data type. Compared to floating-point types, the decimal type has more precision and a smaller range, which makes it appropriate for financial and monetary calculations.

Format a double to two digits after the comma without rounding up or down

I have been searching forever and I simply cannot find the answer, none of them will work properly.
I want to turn a double like 0.33333333333 into 0,33 or 0.6666666666 into 0,66
Number like 0.9999999999 should become 1 though.
I tried various methods like
value.ToString("##.##", System.Globalization.CultureInfo.InvariantCulture)
It just returns garbage or rounds the number wrongly.
Any help please?
Basically every number is divided by 9, then it needs to be displayed with 2 decimal places without any rounding.
I have found a nice function that seems to work well with numbers up to 9.999999999
Beyond that it starts to lose one decimal number. With a number like 200.33333333333
its going to just display 200 instead of 200,33. Any fix for that guys?
Here it is:
string Truncate(double value, int precision)
{
string result = value.ToString();
int dot = result.IndexOf(',');
if (dot < 0)
{
return result;
}
int newLength = dot + precision + 1;
if (newLength == dot + 1)
{
newLength--;
}
if (newLength > result.Length)
{
newLength = result.Length;
}
return result.Substring(0, newLength);
}
Have you tried
Math.Round(0.33333333333, 2);
Update*
If you don't want the decimal rounded another thing you can do is change the double to a string and then get get a substring to two decimal places and convert it back to a double.
doubleString = double.toString();
if(doubleString.IndexOf(',') > -1)
{
doubleString = doubleString.Substring(0,doubleString.IndexOf(',')+3);
}
double = Convert.ToDouble(doubleString);
You can use a if statement to check for .99 and change it to 1 for that case.
Math.Truncate(value * 100)/100
Although I make no guarantees about how the division will affect the floating point number. Decimal numbers can often not be represented exactly in floating point, because they are stored as base 2, not base 10, so if you want to guarantee reliability, use a decimal, not a double.
Math.Round((decimal)number, 2)
Casting to a decimal first will avoid the precision issues discussed on the documentation page.
Math.Floor effectively drops anything after the decimal point. If you want to save two digits, do the glitch operation - multiply then divide:
Math.Floor(100 * number) / 100
This is faster and safer than doing a culture-dependent search for a comma in a double-converted-to-string, as accepted answer suggests.
you can try one from below.there are many way for this.
1.
value=Math.Round(123.4567, 2, MidpointRounding.AwayFromZero) //"123.46"
2.
inputvalue=Math.Round(123.4567, 2) //"123.46"
3.
String.Format("{0:0.00}", 123.4567); // "123.46"
4.
string.Format("{0:F2}", 123.456789); //123.46
string.Format("{0:F3}", 123.456789); //123.457
string.Format("{0:F4}", 123.456789); //123.4568

how to take 6 numbers after the dot - but without round the number?

how to take 6 numbers after the dot - but without round the number ?
for example:
102.123456789 => 102.123456
9.99887766 => 9.998877
in C# winforms
thak's in advance
You can use the Math.Truncate method and a 10^6 multiplier:
decimal x = 102.12345689m;
decimal m = 1000000m;
decimal y = Math.Truncate(m * x) / m;
Console.WriteLine(y); // Prints 102.123456
System.Math.Truncate (102.123456789 * factor) / factor;
In your case factor is 10^6; read more
public decimal TruncateDecimal(decimal decimalToTruncate, uint numberOfDecimalPlacse)
{
decimal multiplication_factor = (decimal)Math.Pow(10.0, numberOfDecimalPlacse);
decimal truncated_value = (long)(multiplication_factor * decimalToTruncate);
return (truncated_value / multiplication_factor);
}
I know this is ugly using strings, but thought I'd put it anyway:
double x = 9.9887766;
string[] xs = x.ToString().Split('.');
double result = double.Parse(xs[0] + "." + xs[1].Substring(0, Math.Min(xs[1].Length, 6)));
Might be a long winded way, but how about turning it into a string, locating the decimal point and then grabbing the string minus anything after the 6th decimal place. You could then turn it back into a decimal.
It's crude but how about:
decimal Number = 102.123456789;
string TruncateTarget = Number.ToString();
decimal FinalValue = Decimal.Parse(TruncateTarget.Substring(0, TruncateTarget.IndexOf('.') +6));

Categories