Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                .wrapper
  .sec
    input#form.form.tar(type="number", value="0") 
    span.unit byte
    
  .sec
    button#change.btn(type="button") 変換
    
  .sec
    #result.result
              
            
!

CSS

              
                .wrapper
  padding 20px
  
.sec
  margin-bottom 20px
  &:last-child
    margin-bottom 0
  
.tar
  text-align right
  
.unit 
  display inline-block
  margin-left 10px
  
.form
  padding 10px

.btn
  cursor: pointer
  &:focus
    outline: none
              
            
!

JS

              
                /**
 * ファイルサイズを適切な単位に変換
 * @param {number} size
 * @return {*}
 */
const UnitChangeForByte = (size) => {
  const { target, unit } = getTarget(size)
const newSize = target !== null ? Math.floor((size / target) * Math.pow(10, 2)) / Math.pow(10, 2) : size

  return newSize + unit
}

/**
 * 入力されたファイルサイズを元に計算につかうバイト数と単位を返却
 * @param {number} size
 * @return {{target, unit}}
 */
function getTarget (size) {
  const kb = 1024
  const mb = Math.pow(kb, 2)
  const gb = Math.pow(kb, 3)
  const tb = Math.pow(kb, 4)

  const returnData = (target, unit) => ({ target, unit })

  if (size >= tb) return returnData(tb, 'TB')
  if (size >= gb) return returnData(gb, 'GB')
  if (size >= mb) return returnData(mb, 'MB')
  if (size >= kb) return returnData(kb, 'KB')

  return returnData(null, 'byte')
}

//===計算===//
const input = $('#form')
const result = $('#result')

$('#change').on('click', ()=>{
  const val = parseInt(input.val(), 10)
  result.text(UnitChangeForByte(parseInt(input.val(), 10)))
  
})
              
            
!
999px

Console