SOLVED

Format number for fixed length field

%3CLINGO-SUB%20id%3D%22lingo-sub-1923549%22%20slang%3D%22en-US%22%3EFormat%20number%20for%20fixed%20length%20field%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1923549%22%20slang%3D%22en-US%22%3E%3CP%3EDear%26nbsp%3BHelper%2C%3C%2FP%3E%3CP%3EI%20have%20a%207-character%20field%2C%20which%20must%20receive%20a%20number.%26nbsp%3B%20The%20length%20of%20the%20field%20is%20fixed%3A%207%20characters.%26nbsp%3B%20For%20instance%2C%20the%20field%20is%20located%20from%20column%2010%20to%2016%20(inclusive)%20as%20in%20the%20following%20example%20(imagine%20an%20Hollerith%20card!!!)%20where%20the%20field%20is%20shown%20with%20equal%20signs%20(i.e.%20%3D).%3C%2FP%3E%3CP%3E123456789%3D%3D%3D%3D%3D%3D7890123%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20first%20character%20must%20be%20a%20'-'%20(i.e.%20minus%20sign)%20for%20a%20negative%20number%2C%20or%20a%20space%20for%20a%20positive%20number.%26nbsp%3B%20the%20number%20can%20be%20any%20real%20number%2C%20positive%20or%20negative.%26nbsp%3B%20The%20number%20must%20be%20rounded%20correctly.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ELet's%20take%20an%20example%3A%20347.2952.%26nbsp%3B%20It%20should%20result%20in%3C%2FP%3E%3CP%3E123456789%20347.307890123%3C%2FP%3E%3CP%3ENote%20that%20the%20number%20has%20been%20rounded%20correctly.%26nbsp%3B%20If%20the%20number%20was%20negative%2C%20it%20would%20be%3C%2FP%3E%3CP%3E123456789-347.307890123%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIf%20the%20number%20was%201.%2C%20the%20result%20would%20be%3C%2FP%3E%3CP%3E123456789%201.00007890123%3C%2FP%3E%3CP%3ENote%20the%20result%20does%20not%20need%20to%20be%20padded%20with%20zero%20to%20the%20right.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHere%20is%20another%20example%3A%200.3472952.%26nbsp%3B%20The%20result%20would%20be%3C%2FP%3E%3CP%3E123456789%200.34737890123%3C%2FP%3E%3CP%3Eor%3C%2FP%3E%3CP%3E123456789%20.3472307890123%3C%2FP%3E%3CP%3Ewhich%20would%20be%20even%20better%20since%20it%20carry%20potentially%20more%20precision.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20am%20seeking%20the%20simplest%20solution%20with%20a%20combination%20of%20TEXT%2C%20LEFT%2C%20RIGHT%2C%20CONCATENATE%2C%20IF%2C%20etc.%20functions.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you%20for%20your%20help.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Egcf1956%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1923549%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EFormulas%20and%20Functions%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1923777%22%20slang%3D%22en-US%22%3ERe%3A%20Format%20number%20for%20fixed%20length%20field%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1923777%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F878621%22%20target%3D%22_blank%22%3E%40gcf1956%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ELets%20say%20you%20have%20a%20number%20such%20as%20347.2952%20in%20cell%20A1.%3C%2FP%3E%0A%3CP%3EEnter%20the%20following%20formula%20in%20B1%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3D%22123456789%22%26amp%3BTEXT(A1%2CIF(A1%26lt%3B0%2C%22%22%2C%22%20%22)%26amp%3BREPT(%220%22%2CMAX(INT(LOG(ABS(A1)))%2B1%2C0))%26amp%3B%22.%22%26amp%3BREPT(%220%22%2C5-MAX(INT(LOG(ABS(A1)))%2B1%2C0)))%26amp%3B%227890123%22%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThis%20can%20be%20filled%20down.%3C%2FP%3E%0A%3CP%3EWarning%3A%20the%20number%20in%20A1%20must%20have%20at%20most%205%20digits%20before%20the%20decimal%20point.%3C%2FP%3E%0A%3CP%3E12345.678%20will%20work%2C%20but%20123456.78%20won't.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1925992%22%20slang%3D%22en-US%22%3ERe%3A%20Format%20number%20for%20fixed%20length%20field%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1925992%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F127945%22%20target%3D%22_blank%22%3E%40Hans%20Vogelaar%3C%2FA%3E%26nbsp%3BThank%20you%20Hans%20for%20your%20quick%20response.%26nbsp%3B%20Yes%2C%20it%20works.%26nbsp%3B%20To%20be%20honest%2C%20I%20am%20a%20bit%20disappointed.%26nbsp%3B%20Actually%2C%20I%20came%20up%20to%20the%20same%20conclusion%3A%20logs%26nbsp%3Bwould%20resolve%20the%20issue.%26nbsp%3B%20It%20is%20hard%20to%20believe%20that%20there%20is%20no%20simpler%20solution%20for%20fixed%20length%20field%2C%20which%20was%20the%20norm%20some%20(long)%20times%20ago.%3C%2FP%3E%3CP%3EActually%2C%20I%20mixed%20two%20things%20in%20my%20question.%26nbsp%3B%20I%20wanted%20positive%20and%20negative%20numbers%20to%20be%20rounded%20the%20same%20way.%26nbsp%3B%20This%20particular%20request%20can%20be%20resolved%20by%20testing%20the%20sign.%26nbsp%3B%20(In%20my%20application%2C%20the%20sum%20of%20all%20numbers%20must%20be%20equal%20to%20zero%20without%20any%20residual.)%26nbsp%3B%20The%20second%20part%20of%20the%20question%20is%20related%20to%20the%20fixed%20length%26nbsp%3Bfield%20and%20here%20again%20for%20me%20the%20use%20of%20LEFT%20or%26nbsp%3BRIGHT%2C%20TEXT%20and%20CONCATENATE%20is%20required%2C%20I%20think.%26nbsp%3B%20It%20is%20too%20bad%20that%20Excel%20does%20not%20have%20a%20simpler%20solution.%26nbsp%3B%20Of%20course%2C%20there%20is%20always%20VBA.%3C%2FP%3E%3CP%3EIf%20you%20have%20some%20second%20thought%2C%20it%20would%20be%20appreciated.%26nbsp%3B%20In%20any%20case%2C%20thank%20you%20very%20much%20for%20your%20working%20solution.%3C%2FP%3E%3CP%3ERegards%2C%3C%2FP%3E%3CP%3Egcf1956%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPS%3A%2060%20years%20ago%2C%20my%20neighbor's%20name%20was%20Vogelaar%20way%20back%20then%20when%20I%20lived%20in%20Uccle%2C%20Belgium.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
New Contributor

Dear Helper,

I have a 7-character field, which must receive a number.  The length of the field is fixed: 7 characters.  For instance, the field is located from column 10 to 16 (inclusive) as in the following example (imagine an Hollerith card!!!) where the field is shown with equal signs (i.e. =).

123456789======7890123

 

The first character must be a '-' (i.e. minus sign) for a negative number, or a space for a positive number.  the number can be any real number, positive or negative.  The number must be rounded correctly.

 

Let's take an example: 347.2952.  It should result in

123456789 347.307890123

Note that the number has been rounded correctly.  If the number was negative, it would be

123456789-347.307890123

 

If the number was 1., the result would be

123456789 1.00007890123

Note the result does not need to be padded with zero to the right.

 

Here is another example: 0.3472952.  The result would be

123456789 0.34737890123

or

123456789 .3472307890123

which would be even better since it carry potentially more precision.

 

I am seeking the simplest solution with a combination of TEXT, LEFT, RIGHT, CONCATENATE, IF, etc. functions.

 

Thank you for your help.

 

gcf1956

 

3 Replies
best response confirmed by gcf1956 (New Contributor)
Solution

@gcf1956 

Lets say you have a number such as 347.2952 in cell A1.

Enter the following formula in B1:

 

="123456789"&TEXT(A1,IF(A1<0,""," ")&REPT("0",MAX(INT(LOG(ABS(A1)))+1,0))&"."&REPT("0",5-MAX(INT(LOG(ABS(A1)))+1,0)))&"7890123"

 

This can be filled down.

Warning: the number in A1 must have at most 5 digits before the decimal point.

12345.678 will work, but 123456.78 won't.

@Hans Vogelaar Thank you Hans for your quick response.  Yes, it works.  To be honest, I am a bit disappointed.  Actually, I came up to the same conclusion: logs would resolve the issue.  It is hard to believe that there is no simpler solution for fixed length field, which was the norm some (long) times ago.

Actually, I mixed two things in my question.  I wanted positive and negative numbers to be rounded the same way.  This particular request can be resolved by testing the sign.  (In my application, the sum of all numbers must be equal to zero without any residual.)  The second part of the question is related to the fixed length field and here again for me the use of LEFT or RIGHT, TEXT and CONCATENATE is required, I think.  It is too bad that Excel does not have a simpler solution.  Of course, there is always VBA.

If you have some second thought, it would be appreciated.  In any case, thank you very much for your working solution.

Regards,

gcf1956

 

PS: 60 years ago, my neighbor's name was Vogelaar way back then when I lived in Uccle, Belgium.

 

@gcf1956 

 

I remember the time when fixed-length fields were standard, but as you have found, Excel does not have built-in support for it. A custom VBA function would be the alternative; the cell formula would look less messy.