If my text reaches 1000 i want it to turn into 1K, and 1200 to be 1.2K. Same with millions.
1000K would turn into 1M, 1100 to turn into 1.1M. Is there anyway to do this. (This is unity c#).
Just create a class that will convert it. I would create a class NumberConverter or something like that and method inside:
public string GetConvertedNumber(float numb)
{
string convertedNumber;
if(numb < 1000)
{
convertedNumber = numb.ToString();
}
else if(numb >= 1000 && numb < 1000000)
{
convertedNumber = $"{(numb / 1000) Round it to 1 place after dot.ToString()}K"
}
}
Related
The rule is if value >= 500,000 it will be rounded up to 1,000,000, if the value < 500,000 it will be rounded down to 000,000
Here an example, if I have value like 4,843,820,00 it will be rounded up to 4,844,000,000
If I have value like 1,136,362,500 it will be rounded down to 1,136,000,000
If I have value like 1,500,000 will be rounded up to 2,000,000 & if I have like 1,450,000 it will be rounded down to 1,000,000
Here is what I tried
String kmbGenerator(number) {
if (number > 999 && number < 99999) {
int resulta = (number / 1000).round();
return "$resulta,000";
} else if (number > 99999 && number < 999999) {
int resulta = (number / 1000).round();
return '${resulta.toStringAsFixed(0)},000';
} else if (number > 999999 && number < 999999999) {
int resulta = (number / 1000000).round();
return "$resulta,000,000";
} else if (number > 999999999) {
int resulta = (number / 1000000000).round();
return "$resulta,000,000,000";
} else {
return number.toString();
}
}
Divide by one million, round, then multiply by one million:
int roundMillions(int value) {
return (value / 1e6).round() * 1000000;
}
main() {
void test(int value) {
print('rounded $value to ${roundMillions(value)}');
}
test(4843820000);
test(1136362500);
test(1500000);
test(1450000);
}
Output:
rounded 4843820000 to 4844000000
rounded 1136362500 to 1136000000
rounded 1500000 to 2000000
rounded 1450000 to 1000000
Before you format round it using num.round() for example if you want to round to millions :
double n = 29971800;
double roundTo = 1000000; //million
print((n/roundTo).round()); //prints 30
you can either multiply it by million and format it or just convert to String and add ',000,000'
I am trying to create a slider where the value gets changed exponentially.
Let’s say the minimum is 0 and the maximum is 100. The first half of the slider should change the value slowly, like 0-10 and afterwards faster. I looked up different sites on StackOverflow but none really made sense to me. There seems to be math operations like pow() and exp(). Best would be to put it all in one function so i can use it for different parameters with a function that looks like this:
function(slidervalue, min, max, factor)
and returns the value.
i have managed to create these 2 functions to solve my problem:
func setParamLog(sliderValue: Float, min: Float, max: Float)->Float
{
// Output will be between minv and maxv
var minv :Float = log(min);
var maxv :Float = log(max);
// Adjustment factor
var scale :Float = (maxv - minv) / (max - min);
return exp(minv + (scale * (sliderValue - min)));
}
func setSliderLog(wpm: Float, min: Float, max: Float)->Float
{
// Output will be between minv and maxv
var minv :Float = log(min);
var maxv :Float = log(max);
// Adjustment factor
var scale :Float = (maxv - minv) / (max - min);
return (((log(wpm) - minv) / scale) + min);
}
it would be great to figure out now how i can adjust the scale factor
You could create a custom UISlider that wraps the functionality you want.
import Foundation
class ExponentialSlider {
...
var exponentialValue: Double {
get {
return exp(Double(self.value))
}
}
}
And then call it directly from the slider like this:
yourSlider.exponentialValue
You could also use the min and max values of the slider to modify the result of the computed property.
what i come up with yet is this function:
func exponentSliderValue(slidervalue: Float, exp: Float, min: Float, max: Float)->Float
{
var diff: Float = max - min
var result: Float = pow(slidervalue / diff, exp)
var endresult: Float = ((result * diff) + min) * 10
endresult.round()
endresult = endresult / 10
if(endresult >= min)
{
endresult = endresult + 0
}
if(endresult < min)
{
endresult = min
}
if(endresult >= max)
{
endresult = max
}
return endresult
}
it works pretty good. what i havent figured out yet is to do the whole thing in the opposite direction. all my sliders have default values.. so.. when i have a value of 40. it should translate to the right position (which is 1000) on the slider by default.. i have no idea yet how to do that. it would be great if someone can give me a hint. thanks
I want to know how am I supposed to count the number of time a loop has repeated itself? More specifically how do I extract and output the number of repeats?
var x = 20
while x < 100 {
x += 10
}
The loop has executed 8 times in order to get x == 100. Is there a way to extract the number '8' so it can be used somewhere else (e.g. to make it a variable elsewhere)?
You said it yourself: you want to count. So count!
var x = 20
var numtimes = 0
while x < 100 {
x += 10
numtimes += 1 // count!
}
numtimes // 8
I have written a program that generates prime numbers . It works well but I want to speed it up as it takes quite a while for generating the all the prime numbers till 10000
var list = [2,3]
var limitation = 10000
var flag = true
var tmp = 0
for (var count = 4 ; count <= limitation ; count += 1 ){
while(flag && tmp <= list.count - 1){
if (count % list[tmp] == 0){
flag = false
}else if ( count % list[tmp] != 0 && tmp != list.count - 1 ){
tmp += 1
}else if ( count % list[tmp] != 0 && tmp == list.count - 1 ){
list.append(count)
}
}
flag = true
tmp = 0
}
print(list)
Two simple improvements that will make it fast up through 100,000 and maybe 1,000,000.
All primes except 2 are odd
Start the loop at 5 and increment by 2 each time. This isn't going to speed it up a lot because you are finding the counter example on the first try, but it's still a very typical improvement.
Only search through the square root of the value you are testing
The square root is the point at which a you half the factor space, i.e. any factor less than the square root is paired with a factor above the square root, so you only have to check above or below it. There are far fewer numbers below the square root, so you should check the only the values less than or equal to the square root.
Take 10,000 for example. The square root is 100. For this you only have to look at values less than the square root, which in terms of primes is roughly 25 values instead of over 1000 checks for all primes less than 10,000.
Doing it even faster
Try another method altogether, like a sieve. These methods are much faster but have a higher memory overhead.
In addition to what Nick already explained, you can also easily take advantage of the following property: all primes greater than 3 are congruent to 1 or -1 mod 6.
Because you've already included 2 and 3 in your initial list, you can therefore start with count = 6, test count - 1 and count + 1 and increment by 6 each time.
Below is my first attempt ever at Swift, so pardon the syntax which is probably far from optimal.
var list = [2,3]
var limitation = 10000
var flag = true
var tmp = 0
var max = 0
for(var count = 6 ; count <= limitation ; count += 6) {
for(var d = -1; d <= 1; d += 2) {
max = Int(floor(sqrt(Double(count + d))))
for(flag = true, tmp = 0; flag && list[tmp] <= max; tmp++) {
if((count + d) % list[tmp] == 0) {
flag = false
}
}
if(flag) {
list.append(count + d)
}
}
}
print(list)
I've tested the above code on iswift.org/playground with limitation = 10,000, 100,000 and 1,000,000.
guys, I'm making simple graph drawer and want to find beautiful values for horizontal lines.
For example, if I have value equals to 72089.601562, beautiful is 70000, or 75000. So, I think that beautifulNumber%5 = 0.
Have you any ideas?
How about this?
#import <math.h>
#import <stdio.h>
#define ROUNDING 5000
int beautify(float input)
{
// Cast to int, losing the decimal value.
int value = (int)input;
value = (value / ROUNDING) * ROUNDING;
if ((int)input % ROUNDING > ROUNDING / 2 )
{
value += ROUNDING;
}
return value;
}
int main()
{
printf("%d\n", beautify(70000.601562)); // 70000
printf("%d\n", beautify(72089.601562)); // 70000
printf("%d\n", beautify(76089.601562)); // 75000
printf("%d\n", beautify(79089.601562)); // 80000
printf("%d\n", beautify(70000.601562)); // 70000
return 0;
}
It depends whether you want a floor value, a ceiling value or just to round to the nearest 5000.
For a floor value:
int beautiful = (int)(floor(ugly / 5000.0) * 5000.0);
For a ceiling value:
int beautiful = (int)(ceil(ugly / 5000.0) * 5000.0);
For rounding:
int beautiful = (int)(round(ugly / 5000.0) * 5000.0);
For making graph lines, I'd probably find the minimum and maximum values you have to graph, start with a floor value for the minimum value and then add your desired interval until you have surpassed your maximum value.
For instance:
float minValue = 2.34;
float maxValue = 7.72;
int interval = 1;
NSMutableArray *horizLines = [NSMutableArray array];
int line = (int)(floor(minValue / interval) * interval);
[horizLines addObject:[NSNumber numberWithInt:line]];
do {
line = (int)(ceil(minValue / interval) * interval);
[horizLines addObject:[NSNumber numberWithInt:line]];
if (minValue >= maxValue) break;
minValue = minValue + interval;
}
Use as needed!
Well, it seems like you'd want it to scale based on the size of the number. If the range only goes to 10, then obviously rounding to the nearest 5,000 doesn't make sense. There's probably a really elegant way to code it using bit shifting but just something like this will do the trick:
float value = 72089.601562
int beautiful = 0;
// EDIT to support returning a float for small numbers:
if (value < 0.2) beautiful = int(value*100)/100.;
else if (value < 2.) beautiful = int(value*10)/10.;
// Anything bigger is easy:
else if (value < 20) beautiful = (int)value;
else if (value < 200) beautiful = (int)value/10;
else if (value < 2000) beautiful = (int)value/100;
else if (value < 20000) beautiful = (int)value/1000;
// etc
Sounds like what you want to do is round to 1 or perhaps 2 significant digits. Rounding to n significant digits is pretty easy:
double roundToNDigits(double x, int n) {
double basis = pow(10.0, floor(log10(x)) - (n-1));
return basis * round(x / basis);
}
This will give you roundToNDigits(74518.7, 1) == 70000.0 and roundToNDigits(7628.54, 1) == 8000.00
If you want to round to 1 or 2 digits (only 2 where the second digit is 5), you want something like:
double roundSpecial(double x) {
double basis = pow(10.0, floor(log10(x))) / 2.0;
return basis * round(x / basis);
}