00001 #!/usr/bin//python 00002 # -*- coding: UTF -*- 00003 00004 ############################################################################ 00005 # Copyright (C) 2008 by Tomáš Košan # 00006 # t.kosan@k25.cz # 00007 # # 00008 # This program is free software; you can redistribute it and#or modify # 00009 # it under the terms of the GNU General Public License as published by # 00010 # the Free Software Foundation; either version 2 of the License, or # 00011 # (at your option) any later version. # 00012 # # 00013 # This program is distributed in the hope that it will be useful, # 00014 # but WITHOUT ANY WARRANTY; without even the implied warranty of # 00015 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # 00016 # GNU General Public License for more details. # 00017 # # 00018 # You should have received a copy of the GNU General Public License # 00019 # along with this program; if not, write to the # 00020 # Free Software Foundation, Inc., # 00021 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # 00022 ############################################################################ 00023 00024 def float2engstr(float_num): 00025 00026 abs_num = abs(float_num) 00027 00028 if abs_num == 0: 00029 return (str(0)) 00030 00031 if abs_num >= 1E-9 and abs_num < 1E-6: 00032 return str(float_num*1E9)+"n" 00033 00034 if abs_num >= 1E-6 and abs_num < 1E-3: 00035 return (str(float_num*1E6)+"u") 00036 00037 if abs_num >= 1E-3 and abs_num < 1: 00038 return (str(float_num*1000)+"m") 00039 00040 if abs_num >= 1 and abs_num < 1000: 00041 return (str(float_num)) 00042 00043 if abs_num >= 1000 and abs_num < 900E3: 00044 return (str(float_num/1000)+"k") 00045 00046 if abs_num >= 900E3 and abs_num < 900E6: 00047 return (str(float_num/1E6)+"M") 00048 00049 00050 00051 if __name__ == "__main__": 00052 float2engstr(9E-9) 00053 float2engstr(100E-6) 00054 float2engstr(5E-3) 00055