Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
If I have an int equal to 8675309 what would be the best/quickest way to convert that into a float equal to 0.8675309f?
Examples:
input as an int: 8675309
output as a float: 0.8675309
input as an int: 4234512
output as a float: 0.4234512
input as an int: 56
output as a float: 0.56
input as an int: 123456
output as a float: 0.123456
input as an int: 654321
output as a float: 0.654321
I've tried mapping the outputs to a value between 0 and 1, but for an RNG setup it tends to have an average of .64 which isn't ideal.
I've used Tanh, Cos, Sin, and other functions to get it mapped, but it seems as though I'm really only wanting to get the exact number with a decimal at the beginning of it so the accuracy stays the same.
I could divide by a large number I suppose, but it wouldn't be consistent as the size of the number would have to change as the RNG output changed in size.
I really don't know why this doesn't have enough information in it. In the first line it is clear what EXACTLY I am looking to do. How can someone be more specific than that?
The way you'd do this on paper is:
Find the number of digits in 8675309
Divide by 10^(number of digits)
You can find the number of digits when written as base 10 either by formatting it as a string and counting the characters (bad), or by taking the log to the base 10 and rounding it up (good):
int input = 8675309;
int numDigits = (int)Math.Ceiling(Math.Log10(input));
Then divide this by 10^numDigits:
float result = input / MathF.Pow(10, numDigits);
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 months ago.
Improve this question
Let's say I have two values (lower boundary = 0.5, upper boundary = 100.0). Then I want to split this range 40 times so that the first is 0.5 and the 40th is 100.0. How can I find the 38 values between 0.5 and 100.0 so that the distance between the values is always the same?
This sounds like a homework? You should be really able to solve this simple problem.
Always split your task into steps. Before you think of any code, you must understand the problem. Then find a solution. You can phrase the solution in words and pseudo code. Finally, you can try to implement this solution:
First you must find the appropriate algorithm:
Identify the input and the output (result):
a) calculate the range of your lower_bound=0.5 (input) and upper_bound=100 (input): range=upper_bound-lower_bound
b) Then divide range by steps=40 (input) to get the interval.
c) Finally, walk from lower_bound to upper_bound by incrementing each step by interval to get each value that belongs to the range.
Implement the algorithm
void Main()
{
double steps = 40;
double lowerBound = 0.5;
double upperBound = 100;
double range = Math.Abs(upperBound - lowerBound);
double interval = range / steps;
var rangeValues = new List<double>();
for (double step = lowerBound; step <= upperBound; step += interval)
{
rangeValues.Add(step);
}
}
Test and improve the algorithm to improve performance and readability. For example rename variables and move code into methods. by moving code into methods that accept the input as parameters, you can convert the hard-coded algorithm into a dynamic one.
Make the algorithm robust (important).
For example, you must ensure that the variable steps is never zero to avoid an DivideByZeroException exception. For this reason you must validate the input before you can run the algorithm.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm new to coding and need to write a program for C#
the goal is to write a program that prompts the user to enter an amount including decimals. and the program gives the user back the remander as money. ie...
quater dimes nickle pennie
First you want to get the users input and store it in a variable.
The input will be text so you have to convert that to a decimal.
Then you will need to do your calculations. That is probably the part you need help on.
Lets say the user entered in 25 dollars and 43 cents - 25.43.
I would first divide the dollars by 4 to get the number or quarters.
Then go from there. You will likely use mod %.
Have fun, im sure you can figure it out if you try.
I think the problem isn't with your int.Parse, but with the way you were concatenating your Console.WriteLine string. Here's the fix, I'm not sure if that's what you were needing.
When you concatenate object to a string, add {}. Starting with the number 0 add those inside depending on how many object you want to add to the string like so: {0}. Once you're done with the string you can add the objects after the it separated by a comma.
int dollar = 100;
decimal quarter = .25m;
decimal dime = .10m;
decimal pennie = .01m;
Console.WriteLine("tell me how much money you have, make sure you include doolars and cents. ");
string userMoney = Console.ReadLine();
double userMoney1 = double.Parse(userMoney);
Console.WriteLine("that equals, {0} ", userMoney);
Console.ReadLine();
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am developing a windows forms application. I have a double value 1.5.That value I need to convert into a two byte array. But when I am converting using BitConverter.GetBytes, getting 8 bytes of data. Please refer my code below.
double d = 1.5;
byte[] array = BitConverter.GetBytes(d);
Double is 8 byte value, so if you have an arbitrary double there's no hope for you; however, if you have some restrictions, e.g. the value is in the [0..100] range and has at most 2 digits after the decimal point, you can encode it:
// presuming source in [0..100] with at most 2 digit after decimal point
double source = 1.5;
// short is 2 byte value
short encoded = (short) (source * 100 + 0.5);
byte[] array = BitConvertor.GetBytes(encoded);
// decode back to double
double decoded = encoded / 100.0;
A double is a 64 bit value that just so happens to be 8 bytes. There's nothing that can be done about this.
A double is 8 bytes in length, so unless you want a specific sub-array out of the 8 you're getting (you probably don't), then this is the wrong way to go about it.
You can cast your variable to a single-precision float. That will of course lose some precision, but you will get 4 bytes instead of 8.
If this is still unacceptable, you need to have an implementation of a half-precision float, which sadly doesn't come out-of-the-box with .NET.
I found an implementation here:
http://sourceforge.net/p/csharp-half/code/HEAD/tree/System.Half
You can use it like this:
var half = (Half)yourDouble;
var bytes = Half.GetBytes(half); // 2 bytes
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a situation on my C programming here and just wondering whether my solution is the correct way:
I have a LED display with particle count sensor and will show 6 digit of seven segment numbers as the count value. The sensor will give voltage input value. The input is from 0V to 10V. So the range of 0V-10V need to be shown in the display as 000000 to 999999 count.
My solution is:
Display number = Input voltage * 99999.9
For example:
Display number = 10.000*99999.9=999999
Display number = 5.500*99999.9=549999
Display number = 2.300*99999.9=229999
Is this the correct solution? I notice that I will get a lot of 9 on the display value.
The most usable and user friendly solution is to ignore the fact that your most significant digit is capable of displaying up to 9 and simply multiply by 10000 unless you desperately need the maxim resolution in which case simply use a scale factor of 100000 and document that your range is 0-9.99999.
My reasoning is that it is better to either loose one digit in the accuracy across the whole range or clip just the maximum value than to have an error across the entire range.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Quick question.
I'm making a diagram so the numbers i'm passing to the function can be anything (depending on zoom and such). Lets say I want to round the number 3.086948353 to 3 but i still want other numbers like 2.199999999 to round to 2.2.
Currently it just looks like this:
Math.Round(value, 10)
You want to do two things in single shot:
Get the integer value if first digit after decimal is 0
Get the rounded value upto last 2 digits if its first digit after decimal is not 0.
For second option you can use:
newValue = Math.Round(value, 2)
Now comes the first requirement:
Once you get the decimal with 2 digits after decimal, get last two digits:
int decimalValue= (int)((newValue - (int)newValue ) * 100);
if(decimalValue < 10)
{
newValue = Math.Floor(value);
}