To check the best answer, scroll down to Paulw11's answer.
(I apologize for any english mistakes, it's not my 1st language)
I need to solve this problem in order to continue to develop my app.
Here, I got a screenshot. (I know, it's ugly, I'm setting the constraints.)
The problem is: even when the number is a integer, it still shows as a rational. (ex.: 4 appears as 4.0, 16 as 16.0)
What I want is:
When the number in the textfield is integer, I want it to appear without the decimal-point. ( 4 appear as 4, 16 appear as 16)
When the number in the textfield is rational, I want it to appear with the decimal-point that belong to it. (4.2 appears as 4.2, 2.5 as 2.5)
What I don't want to happen:
Round any number. This will ruin the math. As I said, 4.22 needs to be 4.22 . But 4.0 needs to be only 4 .
I'll be very grateful for any help, thank you.
You can simply use the %g formatting option:
deltaValueS.text = String(format:"∆ é %g",deltaValue)
For 4.0 this will give "∆ é 4"
For 4.123 this will give "∆ é 4.123"
Do you want something like this?
let numberA: Float = 1.234
let numberB: Float = 7.000
func getStringFromNumber(number: Float) -> String {
if number - Float(Int(number)) == 0 {
return String("\(Int(number))")
} else {
return String("\(number)")
}
}
print(getStringFromNumber(numberA)) // console output: 1.234
print(getStringFromNumber(numberB)) // console output: 7
Related
How do I get a random number in Lua to the eighth decimal?
Example : 0.00000001
I have tried the following and several variations of this but can not get the format i need.
math.randomseed( os.time() )
x = math.random(10000000,20000000) * 0.00000001
print(x)
i would like to put in say 200 and get this 0.00000200
Just grab a random number from 0-9, and slide it down 6 places. You can use format specifiers to create the string representation of the number that you desire. For floats we use %f, and indicate how many decimal places we want to have with an intermediate .n, where n is a number.
math.randomseed(os.time())
-- random(9) to exclude 0
print(('%.8f'):format(math.random(0, 9) * 1e-6))
--> '0.00000400'
string.format("%.8f",math.random())
to help anyone else. my question should have been worded a bit better. i wanted to be able to get random numbers and get it to the 8th decimal place.
but i wanted to be able to have those numbers from 1-10,000 so he is updated how i wanted it and the help of Oka got me to this
math.randomseed(os.time())
lowest = 1
highest = 7000
rand=('%.8f'):format(math.random(lowest, highest) / 100000000)
print(rand)
Hope this helps someone else or if it can be cleaned up please let me know
I have a table defined in a gsp-file. The table has a column with numeric edited numbers. I want to rightAlign them, so that the decimalPoints are all in the same position one under the corresponding one in the preceeding line.
peter
Is this what you are looking for? Try it here.
[
'1.0',
'115.00',
'0.0',
'100.0',
'24.9',
'4.09',
'54.09',
'13452.098',
'134520.098',
'198.0',
'0.98'
].each {
def (whole, fraction) = it.tokenize(/./)
println ( [ whole.padLeft(6), fraction ].join(/./) )
}
//Output
1.0
115.00
0.0
100.0
24.9
4.09
54.09
13452.098
134520.098
198.0
0.98
Assumption:
All Decimal numbers
Max 6 digit whole number
You could use the padLeft method as dmahapatro suggested if you are using a fixed-width font, however, if you are not, you are going to have to use some CSS to format it correctly. I suggest putting everything to the right of the decimal place (including the decimal) in a span, giving it a fixed width and aligning it to the left. Check out the fiddle here: http://jsfiddle.net/fvp3obxr/. You might need to adjust the width depending on how many decimal places you have.
unfortunately with how lengthy this thing is I don't have a fiddle for it, as I didn't build it, but basically all I am trying to do is assure that these values get set to two decimal points, regardless of the value of it. If it's 100, I want it to read 100.00 and that is seemingly the issue I am having. The code for this section, which replaces a template value is this;
if (isLegend) {
if (legendFooterTemplate) {
legendFooterData = legendFooterTemplate.replace("highcharts.value", addCommasToLargeNumber(Math.round(totalValues * 100) / 100));
legendFooterData = legendFooterData.replace("highcharts.percent", Math.round(totalPercent));
legendTable.append("<tr>" + legendFooterData + "</tr>");
}
//make the legend visible
legendTable.css("visibility", "visible");
}
you can see it adds commas to larger numbers and takes that formatted number and plugs it into the template where highcharts.value and highcharts.percent live. I just want to know how I can manipulate the math.round() functions to make it have two decimal points no matter what.
Thank you for any and all help,
Nick G
For percentage use:
this.percentage.toFixed(2)
Example: http://jsfiddle.net/jugal/dTMWP/
For other:
Highcharts.numberFormat(this.y,0)
Example: http://jsfiddle.net/CAKQH/24227/
I´ve read a lot of posts about this but all of them were to limit the number digits to show them(NSString) .In my case I have:
I compare two double values(wich are the "same"), each of them got from different mathematical operations. For example: (4.800000 and 4.800000)
double result1=4.800000, result2=4.800000
//compare the results:
if(result1==result2){
msg.text=#"well done!!";
}else if(result1>result2){
msg.text=#"continue your work";
}
"I´m working with money (4,80€)"
In the msg label i get "continue your work" message, not the "well done". I don´t even know if the comparison is done in a correct way.
I think that the best idea would be to limit 4.800000 to 4.80 in order to delete small values and get a exact comparison.(how could i do this?)
I DONT WANT to limit the number to two digits just to PRINT the solution, I want to WORK with that number.
You can do something like this:
double a = 2.55000, b = 2.55002;
if(fabs(a-b)<1e-3) {
// code here, a == b
} else {
// code here, a != b
}
use floor(<#double#>) to round down OR just subtract them and floor the result.
For a nice answer to this problem see:
https://stackoverflow.com/a/10335601/474896
Which could be summarized as a simple as:
if (fabs(x-y) < FLT_EPSILON) {/* ... */}
However since you you're working with money values you should check out NSDecimalNumber.
Or as Marcus Zarra puts it:
"If you are dealing with currency at all, then you should be using
NSDecimalNumber.".
I am doing the following in actionscript in Coldfusion Flash Forms:
90 / 3.7
Gives me:
24.3243243243243
Whereas the calculator gives me:
24.32432432432432
Note the extra 2 at the end.
So my problem occurs when I am trying to get the original value of 90 by taking the 24.3243243243243 * 3.7 and then I get 89.9999999999 which is wrong.
Why is Actionscript truncating the value and how do I avoid this so I get the proper amount that the calculator gets?
Thanks so much.
Round your number using a routine like this
var toFixed:Function = function(number, factor) {
return (Math.round(number * factor)/factor);
}
Where the factor is 10, 100, 1000 etc, a simple way to think about it is the number of 0's in the factor is the number of decimal places
so
toFixed(1.23341230123, 100) = 1.23
Good explanation of numeric in ActionScript can be found at http://docstore.mik.ua/orelly/web2/action/ch04_03.htm. See section 4.3.2.1. Floating-point precision
A relavant quote:
"In order to accommodate for the minute discrepancy, you should round your numbers manually if the difference will adversely affect the behavior of your code. "