I have a key that has a type of DECIMAL I'm passing this key to a subcomponent, the vaildvalues are set as 600, 800, 1000 etc. when I pass the key I have to convert it to FLOAT but this adds on .00 to then end but I want to have no trailing Zeros. How do I remove these?
Unfortunately, there is no proper number to string function at the moment. What I usually do at the moment:
substring(string(number), 0, size(string(number)) - 3)
Related
Can anyone explain why the result is 252.99999999999997 and not 253? What should be used instead to get 253?
double x = 2.11;
double y = 0.42;
print(((x + y) * 100)); // print 252.99999999999997
I am basically trying to convert a currency value with 2 decimal (ie £2.11) into pence/cent (ie 211p)
Thanks
In short: Because many fractional double values are not precise, and adding imprecise values can give even more imprecise results. That's an inherent property of IEEE-754 floating point numbers, which is what Dart (and most other languages and the CPUs running them) are working with.
Neither of the rational numbers 2.11 and 0.42 are precisely representable as a double value. When you write 2.11 as source code, the meaning of that is the actual double values that is closest to the mathematical number 2.11.
The value of 2.11 is precisely 2.109999999999999875655021241982467472553253173828125.
The value of 0.42 is precisely 0.419999999999999984456877655247808434069156646728515625.
As you can see, both are slightly smaller than the value you intended.
Then you add those two values, which gives the precise double result 2.529999999999999804600747665972448885440826416015625. This loses a few of the last digits of the 0.42 to rounding, and since both were already smaller than 2.11 and 0.42, the result is now even more smaller than 2.53.
Finally you multiply that by 100, which gives the precise result 252.999999999999971578290569595992565155029296875.
This is different from the double value 253.0.
The double.toString method doesn't return a string of the exact value, but it does return different strings for different values, and since the value is different from 253.0, it must return a different string. It then returns a string of the shortest number which is still closer to the result than to the next adjacent double value, and that is the string you see.
I have a textedit component and a button component. The button component will add the textedit component text value to a list if that value is greater than 0. On the textedit component I can as many decimal places as I want, but I'd like to validate two decimal places. Like If I put 00.0032 in the textedit component the validation will take that as 0. Is there a function that will allow me to do this or do I have to do this by my own code.
This is my code
if (Trim(textEdit.Text) <> '') and (StrToCurr(Trim(textEdit.Text)) <> 0) then
begin
code to add the value
end;
Reading your question two possible solutions come to my mind:
You could convert to float multiply by 100 (to shift by two decimals) and round using floor:
(Floor(StrToFloat(Trim(textEdit.Text)) * 100) <> 0)
This performs a conversion to floating point which might be slow.
An other solution could be to use string functions:
(StrToCurr(Copy(textEdit.Text, 1, Pos('.', textEdit.Text) + 2)) <> 0)
This copies the input string from beginning to two digits after the decimal separator '.'.
Don't worry if your string is shorter (for example '0.1') you won't get an error.
Which solution is ultimately fast would have to be benchmarked.
Also have in mind, that not in every region a '.' is the decimal separator.
In most of Europe for example decimal separator is ',' and thousands separator is '.'.
Find out about TFormatSettings.
PS: You don't need to Trim before using StrToCurr because it does a trim internally.
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.
NSLog(#"CEIL %f",ceil(2/3));
should return 1. However, it shows:
CEIL 0.000000
Why and how to fix that problem? I use ceil([myNSArray count]/3) and it returns 0 when array count is 2.
The same rules as C apply: 2 and 3 are ints, so 2/3 is an integer divide. Integer division truncates so 2/3 produces the integer 0. That integer 0 will then be cast to a double precision float for the call to ceil, but ceil(0) is 0.
Changing the code to:
NSLog(#"CEIL %f",ceil(2.0/3.0));
Will display the result you're expecting. Adding the decimal point causes the constants to be recognised as double precision floating point numbers (and 2.0f is how you'd type a single precision floating point number).
Maudicus' solution works because (float)2/3 casts the integer 2 to a float and C's promotion rules mean that it'll promote the denominator to floating point in order to divide a floating point number by an integer, giving a floating point result.
So, your current statement ceil([myNSArray count]/3) should be changed to either:
([myNSArray count] + 2)/3 // no floating point involved
Or:
ceil((float)[myNSArray count]/3) // arguably more explicit
2/3 evaluates to 0 unless you cast it to a float.
So, you have to be careful with your values being turned to int's before you want.
float decValue = (float) 2/3;
NSLog(#"CEIL %f",ceil(decValue));
==>
CEIL 1.000000
For you array example
float decValue = (float) [myNSArray count]/3;
NSLog(#"CEIL %f",ceil(decValue));
It probably evaluates 2 and 3 as integers (as they are, obviously), evaluates the result (which is 0), and then converts it to float or double (which is also 0.00000). The easiest way to fix it is to type either 2.0f/3, 2/3.0f, or 2.0f/3.0f, (or without "f" if you wish, whatever you like more ;) ).
Hope it helps
I want to define a percentage type.
TPrecent = 0 .. 100; works fine, but I can only assign integers to it an dwoudl also like to assign floats.
How can I do that?
Ranges can only be integer ordinal types. You will need to use a float variable and validate the value yourself.
TPercent is an integer sub-range. You cannot create a sub-range that is a floating point underlying type.