![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
|
Hi all, I need a calculated result to be rounded in 4 significant digits. Just to make it clear, significant digits are any digits excluding leading zeroes. E.g. in 0.0045717 the first significant digit is 4 and my desired result would be 0.004572. The solution I found was the following : Position_calc: Min( Case(Position(field; 1 ; 1 ; 1 )=0;30;Position(field; 1 ; 1 ; 1 )); Case(Position(field; 2 ; 1 ; 1 )=0;30;Position(field; 2 ; 1 ; 1 )); // and so on up to Case(Position(field; 9 ; 1 ; 1 )=0;30;Position(field ; 9 ; 1 ; 1 )) ) This checks the number for the first significant digit (1-9). Because the absence of any of the digits 1-9 will return a 0-value I check for this with the Case-statements and, if the value is 0, set it to 30 (arbitrary number, just to prevent getting a 0-result). Then, to round the field correctly, I use another calculation : Round(field;Position_calc + 2) It works OK, but I would like to know if there is another/easier/faster way to do this. Secondly, this doesn't work if the last significant digit is a "0" because Filemaker just discards any trailing zeroes, although they are significant. Thanks, Peter |
#2
| |||
| |||
|
|
Hello Peter First an important warning: At least in FMP v 4.0 the Round function is totally buggy. Never use it! See www.tekstotaal.com/c.fmp.truncate.bug.html I read some contradiction in your question: You write that trailing zeroes are not signficant and later on you say that they are indeed significant. As the the core question: Why not multiply by e.g. 1000000000, cut/round everything behind the first 4 digits and the divide again by 1000000000 ? Or am I mistaken here |
#3
| |||
| |||
|
|
Hi Christoph, thanks for answering and pointing out the Round-bug. I'm using V5 so I hope its behaviour has improved. As to the seeming contradiction: I initially indicated that *leading* zeroes (0 before the first non-zero digit) were insignificant, but *trailing* zeroes (zero after the 3rd non-zero digit) were not. I'm afraid your suggestion of multiplying/dividing would not work. Just a simple test : 0.045678945 * 10000000000 = 456789450 4567/10000000000 = 0.0000004567 Thanks again, Peter On Wed, 6 Aug 2003 17:59:24 +0200, Christoph Bouthillier wrote: Hello Peter First an important warning: At least in FMP v 4.0 the Round function is totally buggy. Never use it! See www.tekstotaal.com/c.fmp.truncate.bug.html I read some contradiction in your question: You write that trailing zeroes are not signficant and later on you say that they are indeed significant. As the the core question: Why not multiply by e.g. 1000000000, cut/round everything behind the first 4 digits and the divide again by 1000000000 ? Or am I mistaken here |
#4
| |||
| |||
|
|
Assuming fields: Number (number) SignificantDigits (number) Calculation (calculation, number) = Int( (Number * (10 ^ ((Position( Number, Left( Substitute( Substitute( Number, ".", ""), "0", ""), 1), 1, 1)) - (Case( PatternCount( Number, "."), Position( Number, ".", 1, 1), Length( Number) + 1)) + SignificantDigits - ((Position( Number, Left( Substitute( Substitute( Number, ".", ""), "0", ""), 1), 1, 1)) > (Case( PatternCount( Number, "."), Position( Number, ".", 1, 1), Length( Number) + 1)))))) + .5) / (10 ^ ((Position( Number, Left( Substitute( Substitute( Number, ".", ""), "0", ""), 1), 1, 1)) - (Case( PatternCount( Number, "."), Position( Number, ".", 1, 1), Length( Number) + 1)) + SignificantDigits - ((Position( Number, Left( Substitute( Substitute( Number, ".", ""), "0", ""), 1), 1, 1)) > (Case( PatternCount( Number, "."), Position( Number, ".", 1, 1), Length( Number) + 1))))) "Peter" <nomail (AT) homeorwork (DOT) tnx> wrote in message news:bgr2g3$89f$1 (AT) reader08 (DOT) wxs.nl... Hi all, I need a calculated result to be rounded in 4 significant digits. Just to make it clear, significant digits are any digits excluding leading zeroes. E.g. in 0.0045717 the first significant digit is 4 and my desired result would be 0.004572. The solution I found was the following : Position_calc: Min( Case(Position(field; 1 ; 1 ; 1 )=0;30;Position(field; 1 ; 1 ; 1 )); Case(Position(field; 2 ; 1 ; 1 )=0;30;Position(field; 2 ; 1 ; 1 )); // and so on up to Case(Position(field; 9 ; 1 ; 1 )=0;30;Position(field ; 9 ; 1 ; 1 )) ) This checks the number for the first significant digit (1-9). Because the absence of any of the digits 1-9 will return a 0-value I check for this with the Case-statements and, if the value is 0, set it to 30 (arbitrary number, just to prevent getting a 0-result). Then, to round the field correctly, I use another calculation : Round(field;Position_calc + 2) It works OK, but I would like to know if there is another/easier/faster way to do this. Secondly, this doesn't work if the last significant digit is a "0" because Filemaker just discards any trailing zeroes, although they are significant. Thanks, Peter |
#5
| |||
| |||
|
|
Hi all, I need a calculated result to be rounded in 4 significant digits. Just to make it clear, significant digits are any digits excluding leading zeroes. E.g. in 0.0045717 the first significant digit is 4 and my desired result would be 0.004572. The solution I found was the following : Position_calc: Min( Case(Position(field; 1 ; 1 ; 1 )=0;30;Position(field; 1 ; 1 ; 1 )); Case(Position(field; 2 ; 1 ; 1 )=0;30;Position(field; 2 ; 1 ; 1 )); // and so on up to Case(Position(field; 9 ; 1 ; 1 )=0;30;Position(field ; 9 ; 1 ; 1 )) ) This checks the number for the first significant digit (1-9). Because the absence of any of the digits 1-9 will return a 0-value I check for this with the Case-statements and, if the value is 0, set it to 30 (arbitrary number, just to prevent getting a 0-result). Then, to round the field correctly, I use another calculation : Round(field;Position_calc + 2) It works OK, but I would like to know if there is another/easier/faster way to do this. Secondly, this doesn't work if the last significant digit is a "0" because Filemaker just discards any trailing zeroes, although they are significant. Thanks, Peter |
#6
| |||
| |||
|
|
Hi all, I need a calculated result to be rounded in 4 significant digits. Just to make it clear, significant digits are any digits excluding leading zeroes. E.g. in 0.0045717 the first significant digit is 4 and my desired result would be 0.004572. The solution I found was the following : Position_calc: Min( Case(Position(field; 1 ; 1 ; 1 )=0;30;Position(field; 1 ; 1 ; 1 )); Case(Position(field; 2 ; 1 ; 1 )=0;30;Position(field; 2 ; 1 ; 1 )); // and so on up to Case(Position(field; 9 ; 1 ; 1 )=0;30;Position(field ; 9 ; 1 ; 1 )) ) This checks the number for the first significant digit (1-9). Because the absence of any of the digits 1-9 will return a 0-value I check for this with the Case-statements and, if the value is 0, set it to 30 (arbitrary number, just to prevent getting a 0-result). Then, to round the field correctly, I use another calculation : Round(field;Position_calc + 2) It works OK, but I would like to know if there is another/easier/faster way to do this. Secondly, this doesn't work if the last significant digit is a "0" because Filemaker just discards any trailing zeroes, although they are significant. Thanks, Peter |
#7
| |||
| |||
|
|
Hi Peter Here is my offering: Left(Number, Position(Number, ".", 1, 1) - 1) & Round("." & TextToNum(Right(Number, Length(Number) - Position(Number, ".", 1, 1))),SignificantDigits) / TextToNum(Left("10000000000000000000", Length(Right(Number, 1 + Length(Number) - Position(Number, ".", 1, 1))) - Length(NumToText(TextToNum(Right(Number, Length(Number) - Position(Number, ".", 1, 1))))))) I tackled it predominantly as a text problem. I began by using the right function to grab everything after the decimal point (=X). Then I converted the text to a number to lose the leading zeroes (=Y), then rounded the result using Glenn's SignificantDigits field as the number parameter (=Z). Then, to reinsert the leading zeroes, I divided Z by a figure that I obtained using Left to extract characters to the value of the difference between the length of X and the length of Y (which = the number of leading zeroes) plus 1 from "100000000000000000000000". Finally, I added anything that happened to be before the decimal place in the event that number is greater than one or in the negative range. You don't say whether the calc needs to be able to do this. NB: I have given the text parameter of the Left function only 20 digits, but this could be extended theoretically to the max no of characters that that parameter will take which is 253. Bridget Eley in article bgr2g3$89f$1 (AT) reader08 (DOT) wxs.nl, Peter at nomail (AT) homeorwork (DOT) tnx wrote on 7/8/03 12:13 AM: Hi all, I need a calculated result to be rounded in 4 significant digits. Just to make it clear, significant digits are any digits excluding leading zeroes. E.g. in 0.0045717 the first significant digit is 4 and my desired result would be 0.004572. The solution I found was the following : Position_calc: Min( Case(Position(field; 1 ; 1 ; 1 )=0;30;Position(field; 1 ; 1 ; 1 )); Case(Position(field; 2 ; 1 ; 1 )=0;30;Position(field; 2 ; 1 ; 1 )); // and so on up to Case(Position(field; 9 ; 1 ; 1 )=0;30;Position(field ; 9 ; 1 ; 1 )) ) This checks the number for the first significant digit (1-9). Because the absence of any of the digits 1-9 will return a 0-value I check for this with the Case-statements and, if the value is 0, set it to 30 (arbitrary number, just to prevent getting a 0-result). Then, to round the field correctly, I use another calculation : Round(field;Position_calc + 2) It works OK, but I would like to know if there is another/easier/faster way to do this. Secondly, this doesn't work if the last significant digit is a "0" because Filemaker just discards any trailing zeroes, although they are significant. Thanks, Peter |
#8
| |||
| |||
|
|
Your calc is graceful. However, when I tried it, I didn't get the trailing zeros to four significant digits. |
#9
| |||
| |||
|
|
F'rinstance, 5.00023000999 calculates to 5.00023 instead of 5.0002300 From Peter's post, "As to the seeming contradiction: I initially indicated that *leading* zeroes (0 before the first non-zero digit) were insignificant, but *trailing* zeroes (zero after the 3rd non-zero digit) were not." |
#10
| |||
| |||
|
|
Where my calc does fall down is on whole numbers (returned ?), and negative numbers (rounding up instead of down and down instead of up), both easy enough to fix. |
![]() |
| Thread Tools | |
| Display Modes | |
| |