Number With Decimal Point

how to make a number with a decimal point?
43 Replies
Pszczółka Erina
I have a question because there are 3 different decimal points: float, double, and decimal Which one is the best to encode a number with 3 digits before the decimal point and 2 digits after the decimal point any number between 100.01 and 999.99 with exactly 3 digits before the decimal point and exactly 2 digits after the decimal point
Pope
Pope3d ago
not one is best. They all have different purposes.
Pszczółka Erina
i want the number? which one will give the correct precision?
Pope
Pope3d ago
float gives about 6 decimal places, double 10, decimal 15
Pszczółka Erina
so that 999.99 won't suddenly turn into 1000
Cattywampus
Cattywampus3d ago
based on the example you want decimal likely
cathei
cathei3d ago
Integer
Pope
Pope3d ago
float is fine with 999.99
Pszczółka Erina
is float fine with 999.99 + 0.01 = 1000?
MODiX
MODiX3d ago
Pope
REPL Result: Success
var x = 999.99f;
for (var i = 0; i < 10_000; i++)
{
x += 0.00001f;
}
Console.WriteLine(x);
var x = 999.99f;
for (var i = 0; i < 10_000; i++)
{
x += 0.00001f;
}
Console.WriteLine(x);
Console Output
999.99
999.99
Compile: 442.415ms | Execution: 47.578ms | React with ❌ to remove this embed.
MODiX
MODiX3d ago
Cattywampus
REPL Result: Success
Console.WriteLine(999.99f + 0.01f);
Console.WriteLine(999.99f + 0.01f);
Console Output
1000
1000
Compile: 387.629ms | Execution: 25.851ms | React with ❌ to remove this embed.
cathei
cathei3d ago
You should state the purpose of this number
Pope
Pope3d ago
It really depends on what you're planning on doing with them.
Pszczółka Erina
for counting stuff like money, or amount of something...
Pope
Pope3d ago
If it's money, use decimal.
Cattywampus
Cattywampus3d ago
^
Pope
Pope3d ago
If it's quantity, maybe double is fine.
Pszczółka Erina
but it's for a temporary mantissa without exponent and then the main money would be decimal
Pope
Pope3d ago
If you're going to be doing exponentiation with it, double might be the best.
Pszczółka Erina
is float ok for the mantissa or is it double?
Pope
Pope3d ago
if it's for money, just use long cents.
Muhammad Ali
Muhammad Ali3d ago
I never used float but decimal provides you more precise value in c# by using celing and floor software based functions. but double use binary operations and provide you accurate value in most efficient way.
cathei
cathei3d ago
decimal type is pretty much for money
Pope
Pope3d ago
decimal type is denoted by m, which stands for money
Cattywampus
Cattywampus3d ago
fucking Pope typing super quick
Pszczółka Erina
I know but I want like a int 12345 (mantissa) turn into 123.45 times 1000^n how to store 123.45
Sehra
Sehra3d ago
or using d would be confusing with double, so m it was
Pope
Pope3d ago
it's not or. It is the way it is just how I said. the etymology isn't of issue here.
Pszczółka Erina
as a temporary variable in a method
Muhammad Ali
Muhammad Ali3d ago
you have to use Math library functions Floor or Ceil for this purpose
cathei
cathei3d ago
Why do you do this
Muhammad Ali
Muhammad Ali3d ago
No datatype will handle this.
Pszczółka Erina
why
Pope
Pope3d ago
It's kind of funny that you use the word mantissa, but don't think about log10 or similar.
cathei
cathei3d ago
Decimal is fine. But why not either just have decimal or handle as integer :catthinking:
Pope
Pope3d ago
Usually I only use the word mantissa when talking about the fractional part of an exponent.
Muhammad Ali
Muhammad Ali3d ago
datatypes are like containers to put a value. It based on you what you put inside. It is not data types duty to perform logic on your value.
Pope
Pope3d ago
and guess what double does under the hood for you anyways
MODiX
MODiX3d ago
Cattywampus
REPL Result: Success
public static double ToFloatingPoint(int value)
{
string a = value.ToString();

if (a.Length < 2)
{
return value;
}

string ab = a.Insert(a.Length - 2, ".");
int exponent = a.Length - 2 -3;
double baseNumber = double.Parse(ab);
double result = baseNumber * Math.Pow(1000, exponent);
return baseNumber;
}
double result = ToFloatingPoint(12345);
Console.WriteLine(result);
public static double ToFloatingPoint(int value)
{
string a = value.ToString();

if (a.Length < 2)
{
return value;
}

string ab = a.Insert(a.Length - 2, ".");
int exponent = a.Length - 2 -3;
double baseNumber = double.Parse(ab);
double result = baseNumber * Math.Pow(1000, exponent);
return baseNumber;
}
double result = ToFloatingPoint(12345);
Console.WriteLine(result);
Console Output
123.45
123.45
Compile: 455.555ms | Execution: 50.875ms | React with ❌ to remove this embed.
Cattywampus
Cattywampus3d ago
you don't? you generally don't want to round for money or else, well.. you'll lose money 🙂
Muhammad Ali
Muhammad Ali3d ago
ummm. but he wants to round it to 2 digits only...
Cattywampus
Cattywampus3d ago
truncation or rounding? those are two different things
Muhammad Ali
Muhammad Ali3d ago
I never read about truncation. So, I don't know about it. Let me check what it is.

Did you find this page helpful?